3

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!

8
// 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');
1

So you can definitely search posts by taxonomy title - custom or otherwise. The answer will be in the "tax_query" part of WP_Query. Here's an example from the Codex, adapted to your needs:

<ul>
<?php

global $post;
$album_title = $_GET['album-title'];
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'tax_query' => array( // NOTE: array of arrays!
        array(
            'taxonomy' => 'albums',
            'field'    => 'name',
            'terms'    => $album_title
        )
    )
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();?>

</ul>

UPDATE

I have not tested this, but in theory, I think it could work. To match anything that contains "foot":

<ul>
<?php

global $post;
$album_title = $_GET['album-title']; // say the user entered 'foot'
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'tax_query' => array( // NOTE: array of arrays!
        array(
            'taxonomy' => 'albums',
            'field'    => 'name',
            'terms'    => $album_title,
            'operator'    => 'LIKE'
        )
    )
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();?>

</ul>

Hope that helps!

  • Thanks!, although I need it to be more of a user based search. So the user would type in the word foot and it would pull in the relevant Football Hits album – DIM3NSION Feb 16 '17 at 21:23
  • Sure - just change the hard-coded title for a variable... How you get that user search value is up to you though... I've updated my answer with one variation, as an example... – MacPrawn Feb 16 '17 at 21:25
  • Thanks MacPrawn, appreciate your help. Although surely the above won't load in the terms Football Hits, if the search value is 'foot'. Maybe I'm being silly – DIM3NSION Feb 16 '17 at 22:04
  • Sorry - I missed the part where you wanted partial matches... I've update my answer. – MacPrawn Feb 16 '17 at 22:51
  • I've tested the above, however I'm looking for it to just pull in the relevant Album Title, not the posts inside that album you see. So not posts categorised in Football Hits, but literally just the album permalink. – DIM3NSION Feb 16 '17 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.