WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to include categories in search results. I've been searching for hours now with no solution.

By "including categories" I don't mean search in a certain category, I mean let's say I have a bicycle store and have many companies included in the site; a user searched for BMX mountain cross for example. It will return the category BMX first (by clicking you'll be sent to the category page) and the posts related to the search term like WordPress normally does.

Does anyone have any clues or could point me to the right direction?

share|improve this question
up vote 1 down vote accepted

I'm using this code in my search.php above the main loop:

$search_term = explode( ' ', get_search_query( false ) );   
global $wpdb;
$select = "
FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt 
ON t.term_id = tt.term_id 
WHERE tt.taxonomy IN ('category')";      
$first = true;
foreach ( $search_term as $s ){
    if ( $first ){
        $select .= " AND (t.name LIKE '%s')";
        $string_replace[] = '%'.$wpdb->esc_like( $s ).'%';
        $first = false;
        $select .= " OR (t.name LIKE '%s')";
        $string_replace[] = '%'. $wpdb->esc_like( $s ).'%';
$select .= " ORDER BY t.name ASC";
$terms = $wpdb->get_results( $wpdb->prepare( $select, $string_replace ) );
if ( count($terms) > 0 ){
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li><a href="'.esc_url( get_term_link( $term ) ).'" title="'.esc_attr( $term->name ).'">' . esc_html( $term->name ) . '</a></li>';
    echo '</ul>';

This code does an extra DB query, but search for categories not only associated to returned posts, but does an extra seach for each word in a search term and brings all found categories - even empty.

share|improve this answer
David thanks. this might do what i am looking for but i've decided to use get_term_by and go from there. i'll accept your answer – Yaniv Kossas Feb 5 '13 at 17:17

Obviously possible, if it works like this, I'm using TwentyTwelve, you have to edit search.php. You will find the loop there:

<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>

So, the loop is taking the post_format(). So you have to edit content.php. Here you will find these:

<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->

Just change that to:

<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_category(); ?><br/>
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->

If everything goes right, than it will echo the Category associated the search result. But if everything goes right like we want. :)

share|improve this answer
that is not what i was looking for at all but thanks. you are simply displaying the category name or the link if for the category if i recall correct, i am not trying to do that. – Yaniv Kossas Feb 5 '13 at 17:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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