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?

7 s
7

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

$search_term = explode( ' ', get_search_query( false ) );   
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[] = '%'.$wpdb->esc_like( $s ).'%';
        $first = false;
    }else{
        $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.

Leave a Reply

Your email address will not be published. Required fields are marked *