Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 0 down vote accepted

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

$search_term = explode( ' ', get_search_query() );  
global $wpdb;
$select = "
SELECT DISTINCT t.*, tt.* 
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[] = '%'.$s.'%';
        $first = false;
    }else{
        $select .= " OR (t.name LIKE '%s')";
        $string_replace[] = '%'.$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="'.get_term_link( $term ).'" title="'.$term->name.'">' . $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
add comment

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
add comment

Your Answer

 
discard

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.