If you want to exclude your custom post type’s posts from search results so the users can only get your pages, posts or category data?
Then you land at the right place
Here:
We have posted the simple snippets that will help you to limit search results to specific post types or category.
Let’s get started.
Limit Search Results in WordPress
To make it easy we have created 3 section for a limit search result.
- Limit Search to Posts and Pages (Except Custom Post type)
- Limit Search to Specific Post Type
- Limit Search to Category
Let’s have a look at that section.
WordPress Limit Search to Only Posts & Pages
In this guide, we are going to display a result of Posts and pages only.
So what we need to do is just copy the below code and paste it into your functions.php file.
After that:
Save the file and view result in the browser.
Limit Search to Custom Post Type
To only search the specific custom post type’s post, we need to use the below codes and do steps.
So just copy the below-embedded code and paste it into your searchform.php file.
After that:
Change the value of line no: 8 value="t-shirts"
to your custom post name.
Let’s move to the specific category.
Limit Search to Specific Category
To only search the specific category’s post, we will use below code.
So just copy the below-embedded code and paste it into your searchform.php file.
After that:
Change the value=”8″
to your Category’s ID.
That’s it.
Hope it helped you with your website project.
Let us know your opinion and error in the comment box.
Don’t forget to share this WordPress Limit Search guide.