post_type );
if ( ! $directory_id ) {
return false;
}
$sections = adqs_get_listing_fields( $directory_id );
return is_array( $sections ) ? $sections : false;
}
// Grid post meta functions
/**
* Get common listing meta data
*
* @param int $post_id The post ID
* @param string $metakey The meta key
* @param bool $single Whether to return a single value (default: true)
* @return mixed The meta value or false on error
*/
function adqs_get_common_listing_meta( $post_id, $metakey, $single = true ) {
if ( empty( $post_id ) || ! is_numeric( $post_id ) || empty( $metakey ) ) {
return false;
}
$value = get_post_meta( $post_id, $metakey, $single );
return apply_filters( 'adqs_common_listing_metadata', $value, $post_id, $metakey );
}
/**
* Render repeated taxonomy terms for a post
*
* @param int $post_id The post ID
* @param string $tax The taxonomy slug (optional, auto-detected if not provided)
* @param int $display_item Number of items to display (currently unused)
* @return array Array of taxonomy terms
*/
function adqs_render_repeated_tax( $post_id, $tax_name = 'categories', $display_item = null ) {
$tax = adqs_get_taxonomy_for_post_type( get_post_type( $post_id ), $tax_name );
$terms = get_the_terms( $post_id, $tax ) ? get_the_terms( $post_id, $tax ) : [];
if ( ! empty( $display_item ) ) {
return array_slice( $terms, 0, $display_item );
}
return $terms;
}
/**
* Get the featured image URL for a listing
*
* @param int $post_id The post ID
* @return string|false Featured image URL or false if not found
*/
function adqs_get_listing_feature( $post_id ) {
if ( empty( $post_id ) || ! is_numeric( $post_id ) ) {
return false;
}
$feature_image = get_the_post_thumbnail_url( $post_id );
return $feature_image ?: false;
}
/**
* Get terms for a taxonomy with default arguments
*
* @param string $taxonomy The taxonomy name
* @param array $args Additional arguments to pass to get_terms
* @return array Array of taxonomy terms
*/
function adqs_get_terms( $taxonomy, $args = [] ) {
return get_terms(
array_merge(
[
'taxonomy' => $taxonomy,
'hide_empty' => true,
],
$args
)
);
}
/**
* Get the current page number for pagination
*
* @return int The current page number
*/
function adqs_post_paged() {
global $paged;
if ( get_query_var( 'paged' ) ) {
$post_page = get_query_var( 'paged' );
} else {
if ( get_query_var( 'page' ) ) {
$post_page = get_query_var( 'page' );
} else {
$post_page = 1;
}
set_query_var( 'paged', $post_page );
$paged = $post_page;
}
return $paged;
}
/**
* Generate pagination links for posts
*
* @param WP_Query|null $query The query object (optional)
* @param string|null $base_url Base URL for pagination (optional)
* @param int|null $current_num Current page number (optional)
* @return void Outputs pagination HTML
*/
function adqs_post_pagination( $query = null, $base_url = null, $current_num = null ) {
global $wp_query;
$big = 999999999;
$base_url = ! empty( $base_url ) ? trailingslashit( $base_url ) . '%_%' : str_replace( $big, '%#%', get_pagenum_link( $big ) );
$current_num = ! empty( $current_num ) ? max( 1, $current_num ) : max( 1, get_query_var( 'paged' ) );
$format = ! empty( $current_num ) ? 'page/%#%/' : '?paged=%#%';
// Preserve search and other query parameters in pagination
$add_args = [];
if ( ! empty( $_GET['ls'] ) ) {
$add_args['ls'] = $_GET['ls'];
}
if ( ! empty( $_GET['listings'] ) ) {
$add_args['listings'] = $_GET['listings'];
}
if ( ! empty( $_GET['directory_type'] ) ) {
$add_args['directory_type'] = $_GET['directory_type'];
}
$paginate_links = paginate_links(
[
'base' => $base_url,
'total' => ( $query != null ) ? $query->max_num_pages : $wp_query->max_num_pages,
'current' => $current_num,
'format' => $format,
'show_all' => false,
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => '',
'next_text' => '',
'add_args' => $add_args,
'add_fragment' => '',
]
);
// Clean up malformed URLs (fix HTML entities and duplicate parameters)
if ( ! empty( $paginate_links ) ) {
$paginate_links = preg_replace( '/#038;/', '&', $paginate_links );
$paginate_links = preg_replace( '/&listings=yes&listings=yes/', '&listings=yes', $paginate_links );
$paginate_links = preg_replace( '/\?listings=yes&listings=yes/', '?listings=yes', $paginate_links );
// Clean up any other malformed URL patterns
$paginate_links = preg_replace( '/\?author=(\d+)&listings=yes&listings=yes/', '?author=$1&listings=yes', $paginate_links );
$paginate_links = preg_replace( '/&paged=(\d+)#qsd_archive/', '&paged=$1#qsd_archive', $paginate_links );
}
if ( ! empty( $paginate_links ) ) {
echo wp_kses_post( $paginate_links );
}
}
/**
* is listing archive
*/
/**
* is listing archive
*/
function adqs_is_listing_archive() {
// Get all dynamic post types
$directory_post_types = adqs_get_directory_post_types();
if ( $directory_post_types && is_post_type_archive( $directory_post_types ) ) {
return true;
}
$directory_taxonomies = adqs_get_directory_taxonomies();
if ( $directory_taxonomies && is_tax( $directory_taxonomies ) ) {
return true;
}
// Check if it's an author listing archive
if ( adqs_is_author_listing_archive() ) {
return true;
}
return false;
}
/**
* Check if current page is an author listing archive
*
* @return bool True if it's an author listing archive, false otherwise
*/
function adqs_is_author_listing_archive() {
// Check if we're on an author page
if ( ! is_author() ) {
return false;
}
// Check if this is a directory listing request
$is_directory_request = false;
// Check if URL contains 'directory' - this is the primary indicator
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '/directory' ) !== false ) {
$is_directory_request = true;
}
// Only check for 'listings' parameter if URL doesn't already contain 'directory'
elseif ( ! empty( $_GET['listings'] ) && $_GET['listings'] === 'yes' ) {
$is_directory_request = true;
}
// Only return true if it's a directory request
return $is_directory_request;
}
/**
* Method adqs_listing_author_url
*
* @param $author_id $author_id [explicite description]
* @param $post_type $post_type [explicite description]
*
* @return string
*/
function adqs_listing_author_url( $author_id = 0, $post_type = '' ) {
if ( empty( $post_type ) ) {
return '';
}
// Get the sanitized author slug
$author_slug = get_the_author_meta( 'user_nicename', $author_id );
if ( empty( $author_slug ) ) {
return '';
}
$author_posts_url = get_author_posts_url( $author_id );
if ( preg_match( '/author\/([a-zA-Z0-9\-_]+)/', $author_posts_url ) && preg_match( '/%postname%/', get_option( 'permalink_structure' ) ) ) {
return ! empty( $author_posts_url ) ? trailingslashit( $author_posts_url ) . 'directory' : '';
} else {
return ! empty( $author_posts_url ) ? add_query_arg( 'listings', 'yes', $author_posts_url ) : '';
}
}