Limit Search to Post Type

If you’re using a lot of custom post types, your search results page could have content you don’t want in it. One solution is to set 'exclude_from_search' => true when registering your post type (more info).

But what if you don’t want to exclude them from all searches – just one specific search? I recently created a Videos section for a client site that pulls in content from the custom post type ‘video’. We wanted a search box on that page that specifically searched through the video post type.

WordPress lets you specify a post type (or multiple post types) by using a hidden field in your search form.

You could use the get_search_form filter to rebuild the search form in that specific context, but here’s a simpler approach:

<?php
// The standard search form
$form = get_search_form( false );
// Let's add a hidden input field
$form = str_replace( '<input type="submit"', '<input type="hidden" name="post_type" value="video"><input type="submit"', $form );
// Display our modified form
echo $form;
view raw functions.php hosted with ❤ by GitHub

I know that the search form will include a submit button, so I’m sticking the hidden input in front of that by doing a find/replace.

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Adam W. Warner says

    Wow, thanks for sharing this Bill. Would it be safe to assume that this could also be adapted to searching all normal posts (not custom post types) and limiting the results to only posts within a certain category or containing a certain tag?

  2. Cathy Tibbles says

    I’m searching in vain in Google to find a way to restrict the search form to tags only… in a WP site (on Genesis). Have you written something on creating/editing the search form beyond the CPT incl/excl?

    Thanks!

  3. Tom Busillo says

    Bill, thanks for this great site and sharing your knowledge.

    I’m working on a site using the genesis framework that has 9,800 custom posts using taxonomies set up using advanced custom fields.

    What I need isn’t just a search box that searches through all of those custom posts, BUT…

    …a full-blown search form that looks something like this:

    name:
    criteria A:
    criteria B:
    criteria C:

    Is there a way to do this using genesis? To make the “search” function really a form that uses a mix of text and dropdowns and that can use either 1 input or up to all 4 inputs from this type of search form?

    Thanks,

    Tom B.

    • Bill Erickson says

      While doable, it’s outside the scope of this post. You’ll need to build a custom search. You can either use WordPress’ search (pass the taxonomies through $_POST[‘your-tax’] to the index.php), or use your own variables and pass them to a custom page template you create, which then takes the data and queries it.

    • Bill Erickson says

      You could use the code shown above to include all post types EXCEPT the one you don’t want. But a better approach is to handle this when you register the post type. One of the parameters of register_post_type() is 'include_in_search' => true. Change that to false and it will be excluded from searches.

  4. Alberto says

    Thank you for this Bill..

    I was searching for a way to differentiate the search actions of my search forms..

    I first thought that woocommerce search form was separate from the WP one but it’s not the case if I am not wrong.

    I mean filtering will impact all search form, right? So this tricks really does the job..

    Thanks a lot

    • Bill Erickson says

      It depends on the context of your code. If you filter get_search_form in functions.php then yes, it will affect all search forms on the site.

      The code snippet above only modifies one specific form on the site. You first get the site-wide form, then use str_replace() to add the post type exclusion. You then echo your customized form.

      For a more complex approach (ex: you need to exclude different post types in different areas of the site) you could set a global variable for excluded post type and check that in your searchform.php file.

      global $be_excluded_post_type;
      $be_excluded_post_type = 'event';
      get_search_form();
      $be_excluded_post_type = false;
      

      Then in searchform.php you check if !empty( $be_excluded_post_type )