Learn how to display the Custom Post Types (CPTs) you decide in WordPress search results using code with this tutorial.
By default, when you search within WordPress the CPTs are excluded from the results.
Do you want your Custom Post Type entries to appear in the search results too?
Here you have how to do it:
Snippet to include CPTs in search results
There are two possible situations:
- That the CPT plugin was created by you.
- Or that it is a plugin from the WordPress repository.
Here is the solution for both possibilities.
1. With you own Custom Post Type Plugin
If you used the tutorial to create a CPT by code, you’re in luck.
You’ll only have to change one line. This:
To see this and another 997 code snippets of this website, login or subscribe here.
Just like that. With this modification your CPT entries will be included in WordPress search results.
2. With a CPT Plugin created by another developer
If the plugin has been developed by a third party, add the following code at the end of functions.php or in your functionality plugin:
To see this and another 997 code snippets of this website, login or subscribe here.
With this code you get posts, pages, products and portfolio entries included in the search results.
Of course, I invite you to change them for the ones you are interested in.
Since this last snippet is not specific to Genesis, you can use it for any WordPress child theme.
Conclusions
Do you want to have more control over which CTPs are displayed in WordPress search results? Now you know how to selectively manage this with code.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂