term_id ) ) { // Get directories assigned to this specific term $term_directories = adqs_get_term_directories( $queried_object->term_id ); if ( ! empty( $term_directories ) && is_array( $term_directories ) ) { $current_directory_id = absint( $term_directories[0] ); } elseif ( isset( $queried_object->taxonomy ) ) { $taxonomy_obj = get_taxonomy( $queried_object->taxonomy ); if ( $taxonomy_obj && ! empty( $taxonomy_obj->object_type ) ) { $post_type = $taxonomy_obj->object_type[0]; if ( adqs_is_directory_post_type( $post_type ) ) { $current_directory_id = adqs_get_directory_id_by_post_type( $post_type ); } } } } } } } /** * Listing Post type widget * */ // Get post type from directory ID $post_type = null; if ( $current_directory_id ) { $post_type = adqs_get_post_type_by_directory( $current_directory_id ); } // Fallback: if no directory detected, get all directory post types (for backward compatibility) if ( ! $post_type ) { $directory_post_types = adqs_get_directory_post_types(); $post_type = ! empty( $directory_post_types ) ? $directory_post_types : 'post'; } $queryArgs = [ 'post_type' => $post_type, 'posts_per_page' => $display_number, ]; // all meta query $metaQuery = []; // Handle related listings for adqs_related_listings widget if ( $widget_id === 'adqs_related_listings' ) { // For related listings, filter by the same post type as the current post $current_post_type = get_post_type( $post_id ); if ( ! empty( $current_post_type ) ) { $queryArgs['post_type'] = $current_post_type; } } if ( ! empty( $order_by ) ) { $queryArgs['orderby'] = $order_by; switch ( $order_by ) { case 'rating-desc': $metaQuery[] = [ 'key' => 'adqs_avg_ratings', 'value' => 0, 'compare' => '>', ]; break; case 'review-count': $queryArgs['comment_count'] = [ 'value' => 0, 'compare' => '>', ]; break; } /** * Short By */ $sortByArgs = adqs_listing_query_sort_by( $order_by ); if ( ! empty( $sortByArgs ) ) { $queryArgs = array_merge( $queryArgs, $sortByArgs ); } } if ( ! empty( $metaQuery ) ) { if ( count( $metaQuery ) > 1 ) { $metaQuery['relation'] = 'AND'; } $queryArgs['meta_query'] = $metaQuery; } if ( $widget_id === 'adqs_related_listings' ) { $taxQuery = []; // Taxonomies are now static and shared across all post types $category_taxonomy = 'adqs_category'; $location_taxonomy = 'adqs_location'; $tag_taxonomy = 'adqs_tags'; // Check each taxonomy type in order of preference $taxonomy_checked = false; // First check category if ( $category_taxonomy ) { $category_terms = $Helper->get_pluck_terms( $post_id, $category_taxonomy, 'term_id' ); if ( ! empty( $category_terms ) ) { $taxQuery[] = [ 'taxonomy' => $category_taxonomy, 'field' => 'term_id', 'terms' => $category_terms, ]; $taxonomy_checked = true; } } // Then check location if ( ! $taxonomy_checked && $location_taxonomy ) { $location_terms = $Helper->get_pluck_terms( $post_id, $location_taxonomy, 'term_id' ); if ( ! empty( $location_terms ) ) { $taxQuery[] = [ 'taxonomy' => $location_taxonomy, 'field' => 'term_id', 'terms' => $location_terms, ]; $taxonomy_checked = true; } } // Finally check tags if ( ! $taxonomy_checked && $tag_taxonomy ) { $tags_terms = $Helper->get_pluck_terms( $post_id, $tag_taxonomy, 'term_id' ); if ( ! empty( $tags_terms ) ) { $taxQuery[] = [ 'taxonomy' => $tag_taxonomy, 'field' => 'term_id', 'terms' => $tags_terms, ]; } } // If no taxonomy terms found, fall back to showing recent listings of the same post type if ( empty( $taxQuery ) ) { // Remove any existing taxonomy queries and just filter by post type // This ensures we still show some relevant listings even without taxonomy matches $queryArgs['orderby'] = 'date'; $queryArgs['order'] = 'DESC'; } else { if ( count( $taxQuery ) > 1 ) { $taxQuery['relation'] = 'OR'; } $queryArgs['tax_query'] = $taxQuery; } } $listItem = new \WP_Query( $queryArgs ); if ( $listItem->have_posts() ) : ?>