Wordpress - Including categories in search results
Use get_terms()
, then you don't need to use a custom query to the database.
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
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>';
}
Based on birgire's answer on a similar question: https://wordpress.stackexchange.com/a/239680/50432
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.