Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify a search query in WordPress?

Tags:

wordpress

I am using WordPress and have a search box in the header. It searches on the basis of posts. Now I want to modify this search box with a cities drop down search box. I also added a new cities table in the database. I need to run the search box on the basis of cities.

like image 700
user1138582 Avatar asked May 04 '26 02:05

user1138582


1 Answers

You can simply add following code in your functions.php file in your WordPress theme directory.

function searchfilter($query) {

if ($query->is_search && !is_admin() ) {
    $query->set('post_type',array('trip'));
}

return $query;
}

add_filter('pre_get_posts','searchfilter');

Here trip is my custom post type and now default search function of WordPress will only search in custom post type trip. You can modify it according to your requirements.

like image 180
Amit Maraiya Avatar answered May 05 '26 14:05

Amit Maraiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!