WordPress Development Asked by Carl Jue Nier on November 21, 2021
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
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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP