• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Thomas Griffin

Be a better leader

  • About
  • Newsletter
  • Articles
  • Business
  • Favorites
  • Contact

How to Include Custom Post Types in WordPress Search Results

Thomas Griffin Avatar
written byThomas Griffin
last modifiedJanuary 29, 2023

This tutorial is a follow up post from my post on how to exclude pages from WordPress search results.

In this tutorial today, we will learn how to include custom post types in WordPress search results by adding modifying the main query via the pre_get_posts hook.

By default, custom post types are not included in the search results, so we are going to create a function that allows them to be queried and searched by WordPress.

How to Include Custom Post Types in WordPress Search Results

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.

Exclusive WordPress Offer

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!

FacebookTweetLinkedIn
Thomas Griffin Avatar
Thomas Griffin

Hello! I'm Thomas, and I've been in WordPress for 10+ years. I've contributed to core and created the TGM Plugin Activation class. My WordPress plugins are active on over 19,000,000 websites and include popular ones like OptinMonster, WPForms and MonsterInsights.

View my WordPress toolbox•More about me →

Reader Interactions

Comments

  1. Patrik CK

    December 28, 2021

    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!

    Reply
    • Thomas Griffin

      January 2, 2022

      Hey Patrick – it could be a number of things, but I would first check if the exclude_from_search parameter is set to true. If it is, it won’t appear in the search results, and you’ll need to change it to false in order for posts in that custom post type to appear.

      Reply
  2. Jared

    January 13, 2022

    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!

    Reply
    • Thomas Griffin

      January 14, 2022

      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.

      Reply
  3. Miranda P

    May 26, 2022

    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!

    Reply
    • Thomas Griffin

      May 28, 2022

      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.

      Reply
  4. Tom

    July 7, 2022

    Thank you for sharing this code! Helped me to add The Event Calendar events to the search. Just “Event” and it worked.

    Reply
    • Thomas Griffin

      July 10, 2022

      Great to hear, Tom! Glad it helped you add your custom event post type to your WordPress search results!

      Reply
  5. Jordan

    December 19, 2022

    Hi Thomas,

    Have you tried this with woocommerce products as I can’t seem to get it to work?

    Many thanks,
    Jordan

    Reply
    • Thomas Griffin

      December 19, 2022

      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!

      Reply
  6. Lock

    December 30, 2022

    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.

    Reply
    • Lock

      December 30, 2022

      With “digression” I meant “aside”. 🙂

      Reply
  7. Lock

    December 30, 2022

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

    Reply
    • Thomas Griffin

      January 2, 2023

      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/

      Reply

Leave a Reply to Thomas Griffin Cancel reply

Your email address will not be published. Required fields are marked *

previous article: How to Exclude Pages from WordPress Search Results
next article: How to Enable HTTP Strict Transport Security (HSTS) in WordPress
Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader

Breakthrough Leadership

Great leaders share common traits that separate them from the pack. Download your copy of my signature ebook, Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader, absolutely FREE when you enter your name and email address below.

If you can dream it, you can do it.

Walt Disney

Footer

ThomasGriffin.com Logo

Copyright © 2010-2023 Griffin Media LLC.
All rights reserved.

Connect

Sites I Like
OptinMonsterTrustPulseWPFormsMonsterInsightsWPBeginnerEclectic TigerSyed BalkhiAwesome MotiveT214

Misc
WordPressPrivacyDisclosures Sitemap

The Better Leader Newsletter

The Better Leader Newsletter

Join 6,889+ leaders taking the right steps to grow in their leadership. No spam - just real, thought-provoking stories and advice to make you a better leader. Grow in your leadership by subscribing today!