Family Encyclopedia >> Electronics

How to limit search results to specific post types in WordPress

Have you ever wondered how you can limit your search results to specific post types? It's not very hard. We have already shown you how to disable the search function in WordPress by modifying the functions.php file. Now we are going to do the same thing, except to filter the search results.

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

 función searchfilter ($ query) if ($ query-> is_search &&! is_admin ()) $ query-> set ('post_type', array ('post', 'page')); devolver $ consulta; add_filter ('pre_get_posts', 'searchfilter'); 

Notice the line that says

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

You can filter the search results by changing the values ​​in the array variable. Right now it's set to show posts and pages, but you can change it to show whatever you want.