categories - How to search only by post title and category?

admin2025-01-07  3

I'm currently working on my first theme (based on underscores) and just realized the search does not return any results if I input a category name, nor does it give accurate results when searching titles.

So, how do I make my theme search only "post titles" and "categories"? Should something be added to the Functions.php?

Thanks

I'm currently working on my first theme (based on underscores) and just realized the search does not return any results if I input a category name, nor does it give accurate results when searching titles.

So, how do I make my theme search only "post titles" and "categories"? Should something be added to the Functions.php?

Thanks

Share Improve this question asked Nov 21, 2016 at 4:17 Carl Jue NierCarl Jue Nier 311 silver badge2 bronze badges 1
  • 3 Possible duplicate of Including categories in search results – Ranuka Commented Nov 21, 2016 at 4:24
Add a comment  | 

1 Answer 1

Reset to default 0

You'll have to write a custom WP_Query in search.php.

Also since you want to search in post_title + categories, you might have to run 2 separate queries and merge results of them into 1.

Something like:

$q1 = get_posts(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    's' => get_search_query()
));
$q2 = get_posts(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'tax_query' => array(
        //YOUR tax query here
    )
));
$merged = array_merge( $q1, $q2 );

And use $merged to show your results.

Also you can also use WPDB to achieve this using MySQL query.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254415a203.html

最新回复(0)