How To Limit Search Results For Specific Post Types in WordPress

Have you ever wondered how you can limit your search results to specific post types? Its not very hard. We’ve already showed you how to disable the search feature in WordPress by modifying the functions.php file. Now we’re going to do the same thing except to filter our search results.

Open your functions.php file and add the following codes:

12345678910functionsearchfilter($query) {    if($query->is_search && !is_admin() ) {        $query->set('post_type',array('post','page'));    }return$query;}add_filter('pre_get_posts','searchfilter');

Hosted with ❤️ by WPCode

1-click Use in WordPress

Notice the line that says

1$query->set('post_type',array('post','page'));

Hosted with ❤️ by WPCode

1-click Use in WordPress

You can filter the search results by changing the values in the array variable. Right now it is set to display posts and pages but you can modify it to display anything you want.

Have you ever wondered how you can limit your search results to specific post types? Its not very hard. We’ve already showed you how to disable the search feature in WordPress by modifying the functions.php file. Now we’re going to do the same thing except to filter our search results. Open your…

Have you ever wondered how you can limit your search results to specific post types? Its not very hard. We’ve already showed you how to disable the search feature in WordPress by modifying the functions.php file. Now we’re going to do the same thing except to filter our search results. Open your…

Leave a Reply

Your email address will not be published. Required fields are marked *