From 0919eff441e494572a59bb360c32d3a9ea37b4ef 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 11:57:34 +0600 Subject: [PATCH] fix: order listing locations by hierarchy --- includes/template-functions.php | 74 ++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/includes/template-functions.php b/includes/template-functions.php index cf8abad784..91b6a16715 100644 --- a/includes/template-functions.php +++ b/includes/template-functions.php @@ -140,6 +140,78 @@ function directorist_has_listing_thumbnail( $listing = null ) { return (bool) directorist_get_listing_thumbnail_id( $listing ); } +function directorist_sort_location_terms_by_hierarchy( $terms ) { + if ( ! is_array( $terms ) || count( $terms ) < 2 ) { + return $terms; + } + + $sort_data = []; + + foreach ( $terms as $term ) { + $ancestors = get_ancestors( $term->term_id, ATBDP_LOCATION, 'taxonomy' ); + $root_id = empty( $ancestors ) ? $term->term_id : end( $ancestors ); + $root_term = $root_id === $term->term_id ? $term : get_term( $root_id, ATBDP_LOCATION ); + + $sort_data[ $term->term_id ] = [ + 'root_id' => (int) $root_id, + 'root_name' => ! is_wp_error( $root_term ) && $root_term ? $root_term->name : '', + 'depth' => count( $ancestors ), + 'name' => $term->name, + ]; + } + + usort( + $terms, + function ( $first_term, $second_term ) use ( $sort_data ) { + $first = $sort_data[ $first_term->term_id ]; + $second = $sort_data[ $second_term->term_id ]; + + $root_name_comparison = strcasecmp( $first['root_name'], $second['root_name'] ); + if ( 0 !== $root_name_comparison ) { + return $root_name_comparison; + } + + if ( $first['root_id'] !== $second['root_id'] ) { + return $first['root_id'] <=> $second['root_id']; + } + + if ( $first['depth'] !== $second['depth'] ) { + return $second['depth'] <=> $first['depth']; + } + + $name_comparison = strcasecmp( $first['name'], $second['name'] ); + if ( 0 !== $name_comparison ) { + return $name_comparison; + } + + return $first_term->term_id <=> $second_term->term_id; + } + ); + + return $terms; +} + function directorist_the_locations( $before = '', $sep = ', ', $after = '', $listing_id = null ) { - the_terms( $listing_id, ATBDP_LOCATION, $before, $sep, $after ); + $terms = get_the_terms( $listing_id, ATBDP_LOCATION ); + + if ( is_wp_error( $terms ) || empty( $terms ) ) { + return; + } + + $links = []; + $terms = directorist_sort_location_terms_by_hierarchy( $terms ); + + foreach ( $terms as $term ) { + $link = get_term_link( $term, ATBDP_LOCATION ); + if ( is_wp_error( $link ) ) { + return; + } + + $links[] = ''; + } + + $term_links = apply_filters( 'term_links-' . ATBDP_LOCATION, $links ); + $term_list = $before . implode( $sep, $term_links ) . $after; + + echo apply_filters( 'the_terms', $term_list, ATBDP_LOCATION, $before, $sep, $after ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Preserve the core the_terms() filter contract. }