• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Include Custom Post Types in WordPress search results

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 839 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 839 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. 🙂

CPT PHP Search

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?