From 756530ce6ad175a9afb6a18edd8854de1ad75eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rabbi=20Islam=20Rony=20=E2=9A=A1=EF=B8=8F?= <35329385+RabbiIslamRony@users.noreply.github.com> Date: Sun, 2 Aug 2026 12:17:03 +0600 Subject: [PATCH] fix: filter popular categories by directory --- includes/model/SearchForm.php | 49 +++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/includes/model/SearchForm.php b/includes/model/SearchForm.php index ce6d259af7..65333c6e8a 100644 --- a/includes/model/SearchForm.php +++ b/includes/model/SearchForm.php @@ -833,27 +833,42 @@ public function top_categories() { $top_categories = []; $args = [ - 'type' => ATBDP_POST_TYPE, - 'parent' => 0, - 'orderby' => 'count', - 'order' => 'desc', - 'hide_empty' => 1, - 'number' => (int) $this->popular_cat_num, - 'taxonomy' => ATBDP_CATEGORY, - 'no_found_rows' => true, + 'type' => ATBDP_POST_TYPE, + 'orderby' => 'count', + 'order' => 'desc', + 'hide_empty' => 1, + 'hierarchical' => false, + 'number' => (int) $this->popular_cat_num, + 'taxonomy' => ATBDP_CATEGORY, + 'no_found_rows' => true, + 'directorist_popular_categories_query' => true, + 'meta_query' => [ + 'relation' => 'OR', + [ + 'key' => '_directory_type_' . absint( $this->listing_type ), + 'compare' => 'EXISTS', + ], + [ + 'key' => '_directory_type', + 'value' => 'i:' . absint( $this->listing_type ) . ';', + 'compare' => 'LIKE', + ], + ], ]; - $cats = get_categories( $args ); + $root_categories_only = static function ( $clauses, $taxonomies, $query_args ) { + if ( empty( $query_args['directorist_popular_categories_query'] ) || ! in_array( ATBDP_CATEGORY, $taxonomies, true ) ) { + return $clauses; + } - foreach ( $cats as $cat ) { - $directory_type = get_term_meta( $cat->term_id, '_directory_type', true ); - $directory_type = ! empty( $directory_type ) ? (array) $directory_type : []; - $listing_type_id = $this->listing_type; + $clauses['where'] .= ' AND tt.parent = 0'; - if ( in_array( $listing_type_id, $directory_type ) ) { - $top_categories[] = $cat; - } - } + return $clauses; + }; + + add_filter( 'terms_clauses', $root_categories_only, 10, 3 ); + $top_categories = get_categories( $args ); + remove_filter( 'terms_clauses', $root_categories_only, 10 ); return $top_categories; }