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

1

1 Answer 1

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.

1
  • You solution will not return posts based on post title searched and category selected. In you cases all posts matching a text or all the posts of a category will be return.. It's and OR condition between post title and post category How And condition between post title and post category handled ? Aug 26, 2019 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.