Skip to content

improve taxonomy field search result #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions includes/fields/class-acf-field-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ function get_ajax_query( $options = array() ) {
// update vars
$args['search'] = $s;
$is_search = true;

// prepend ancestor names in taxonomy field search result
add_filter('acf/fields/taxonomy/result', array($this, 'prepend_ancestor_names'), 10, 3);

}

Expand Down Expand Up @@ -214,7 +217,36 @@ function get_ajax_query( $options = array() ) {
return $response;

}
/**
* This function prepend ancestor names to taxonomy child title.
*
* @date 19/3/2023
* @since 6.0.7
*
* @param string $title The term title.
* @param WP_Term $term The term object.
* @param array $field The field settings.
* @return string
*/

function prepend_ancestor_names($title, $term, $field) {

$ancestors = get_ancestors( $term->term_id, $term->taxonomy );

if( !empty($ancestors) ) {
asort($ancestors);
$prefix = "";
foreach($ancestors as $ancestor){
$parent_term = get_term_by( 'term_id', $ancestor, $field['taxonomy'] );
if($term->term_id != $parent_term->term_id)
$prefix .= $parent_term->name . ' » ' ;
}
return $prefix . $term->name;

}
return $title;

}
/**
* Returns the Term's title displayed in the field UI.
*
Expand Down