Search custom taxonomy term by name

I have a custom taxonomy, called albums.

I need to be able to text search the taxonomy term title, obviously this isn’t default WP Search. Just wondering how I’d best tackle this?

Say there is an album called ‘Football Hits’,

I start typing foot and search that, all I need it to appear is the album title and the permalink.

Thanks!

2 s
2

// We get a list taxonomies on the search box
function get_tax_by_search($search_text){

$args = array(
    'taxonomy'      => array( 'my_tax' ), // taxonomy name
    'orderby'       => 'id', 
    'order'         => 'ASC',
    'hide_empty'    => true,
    'fields'        => 'all',
    'name__like'    => $search_text
); 

$terms = get_terms( $args );

 $count = count($terms);
 if($count > 0){
     echo "<ul>";
     foreach ($terms as $term) {
       echo "<li><a href="".get_term_link( $term )."">".$term->name."</a></li>";

     }
     echo "</ul>";
 }

}

// sample
get_tax_by_search('Foo');

Leave a Comment