This tutorial is a follow up post from my post on how to exclude pages from WordPress search results.
In this tutorial today, you will learn how to include custom post types in WordPress search results by adding modifying the main query via the pre_get_posts
hook.
Key Takeaways
- By default, custom post types are not included in WordPress search results.
- In this step-by-step tutorial, you are going to create a function that allows them to be queried, searched and displayed by WordPress.
Setting up the Function to Include Custom Post Types in WordPress Search Results
Open up your functions.php
file and paste in the following:
/**
* This function modifies the main WordPress query to include an array of
* post types instead of the default 'post' post type.
*
* @param object $query The main WordPress query.
*/
function tg_include_custom_post_types_in_search_results( $query ) {
if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
$query->set( 'post_type', array( 'post', 'movies', 'products', 'portfolio' ) );
}
}
add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_search_results' );
This function is simply filtering our search results by adding new arguments to the query results. As you can see from above, the following function will return content from each of these custom post types: post
, movies
, products
and portfolio
.
Simply change and/or delete the names of the custom post types in the array to match your custom post types that you want included in your WordPress search results.
Want a WordPress website that’s secure AND fast? My friends at WP Engine are offering 3 months free on all annual plans. Click here to claim your special WP Engine offer!
There you have it – now you have included custom post types in your WordPress search results!
Hope you enjoyed the post, and don’t forget to subscribe to get access to more great WordPress content!
Patrik CK
Hi, this code works great, thank you!
I was curious when I use the code I see all the custom post types I’ve created except one. It’s the only one that has a dash in it eg (case-study), could having the dash be why this is the only post type not to appear in my search?
Thanks in advance!
Thomas Griffin
Hey Patrick – it could be a number of things, but I would first check if the
exclude_from_search
parameter is set totrue
. If it is, it won’t appear in the search results, and you’ll need to change it tofalse
in order for posts in that custom post type to appear.Jared
A little out of bounds for the post, I’m sure, but figured it can’t hurt to ask –
Any way to include tags among the search results with the pre_get_posts action? In other words, if the search query/keywords match the tag name of a given post, that post should be returned among the results as well.
I sincerely appreciate the consideration regardless, Thomas. Thanks!
Thomas Griffin
Hey Jared,
Great question! Unfortunately, because of how the main query object works, you cannot easily append posts with are tagged with the search result. It would require a
UNION
query to join the results, and that requires that you filter in an entirely new query altogether. You might want to check out SearchWP which allows you to do some pretty advanced filtering of your search results.Miranda P
Hi Thomas, this is awesome, thank you! I didn’t know it could be this easy 🙂
I think it’s only searching in the titles of my custom posts, not the other fields. Do you know if there’s a way to have it search through all the fields, or to specify additional fields?
Thanks a lot!
Thomas Griffin
Hey Miranda,
Thanks for the kind words — and glad you found including custom post types in your WordPress search results easy!
For the second question, WordPress doesn’t do that by default. I recommend the SearchWP plugin for this, as it’s the best WordPress search plugin and allows you to create custom engines that can search your additional custom post type content and display it in the results.
Tom
Thank you for sharing this code! Helped me to add The Event Calendar events to the search. Just “Event” and it worked.
Thomas Griffin
Great to hear, Tom! Glad it helped you add your custom event post type to your WordPress search results!
Jordan
Hi Thomas,
Have you tried this with woocommerce products as I can’t seem to get it to work?
Many thanks,
Jordan
Thomas Griffin
It should work just fine with WooCommerce as well. You would add
product
to the array of post type slugs. Alternatively, you could use a plugin like SearchWP to have total control over search result customization — and it will search for things like product meta too!Lock
Hi and thanks for the article.
Unfortunately, being a noob with WP, I’m not able to make it works.
I added your function to functions.php and added a “digression” post type inside it.
Then I tried to show all digression post types with this query:
[website url]/?post_type=digression
But it shows all the articles. Any idea why?
Thanks.
Lock
With “digression” I meant “aside”. 🙂
Lock
Hmmm… Maybe I understood what your function does: it only searches the post types you specific inside (right?).
What I need is a way to only show one specific post type, using a specific parameter (I want to list all the aside articles with a link). The search should still search everywhere. 🙂
Thomas Griffin
For that request, this tutorial won’t work. I think you’ll need something custom for this. You could always use SearchWP and configure one of the engines to do this kind of query parameter search for you. That’s the route I would recommend.
Here’s an example of how you could move in that direction: https://searchwp.com/v3/docs/kb/how-to-change-your-search-query-parameter-from-s-to-something-else/