This snippet restricts WordPress search results to specific post types.
Instead of searching across all content, it limits results to only the post types you define, helping keep search results more relevant and intentional.
Copy & paste the scripts before the </body> tag of your project. If you added them before for another setup, skip this step.
Right-click in Elementor, choose “Paste from another site,” and while the popup is open, press cmd/ctrl + v to insert the layout.
Place the code in an HTML widget or add it through Elementor → Custom Code (before the closing </body> tag) either globally or only on selected pages.
Paste the code through the page or site settings, or add it via Elementor → Custom Code (before </body>) for broader use.
Paste the script through Elementor → Custom Code (set to load after </body>) for site-wide or page-specific loading.
Place the PHP snippet inside your theme’s functions.php or using any code snippet to enable logic.
if (!is_admin()) {
function mv_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', array('post','product')); // change post slugs here
}
return $query;
}
add_filter('pre_get_posts', 'mv_search_filter');
}You can control which content appears in search results by editing the post_type array in the code. This array accepts post type slugs, which are the internal identifiers WordPress uses for content types, such as post, page, product, or any custom post type.
To include additional content types, add their slugs to the array.
For example, adding page will include pages in search results, while adding a custom post type slug like portfolio or case-studies will include that content as well.
Removing a slug from the array will exclude that post type from search results.