Make categories appear in search results in Wordpress

Asked

Viewed 30 times

0

2 answers

1

A good option is to install the plugin Relevanssi. It has settings to help with your problem, and also greatly improves the quality of searches.

0


I GOT IT WITH THAT CODE!

$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>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.