InsideDarkWeb.com

How to search only by post title and category?

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

WordPress Development Asked by Carl Jue Nier on November 21, 2021

1 Answers

One Answer

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.

Answered by Aniruddha Gawade on November 21, 2021

Add your own answers!

Related Questions

How can I configure an SMTP Server?

1  Asked on February 11, 2021 by ellen

     

No more get_option(‘date_time’) in WordPress 5.5?

1  Asked on February 10, 2021 by wittich

       

How should I tackle this situation?

0  Asked on February 9, 2021 by remus

   

How to create an anonymous forum?

0  Asked on February 8, 2021 by keyrands

   

Detect if REST API is running

2  Asked on February 8, 2021 by tom-j-nowell

 

Create a bulk action to pass user profiles into a CPT

0  Asked on February 6, 2021 by zaesar

     

Can’t update WordPress with Admin Users

0  Asked on February 4, 2021 by charlene-the-web-design-queen

       

Get Child Category only

1  Asked on February 2, 2021 by joamika

   

MULTISITE: Password issue and Error Cant access site

3  Asked on January 31, 2021 by msn_administrator

 

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.