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; |
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.
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?
Bill Erickson says
Yep, for all normal posts just use “post,page” as the value of the post_type hidden field. For categories and tags, use hidden fields cat and tag.
You can experiment with the results of your search by simply adding it to the end of your search results URL (because that’s all these hidden fields are doing).
https://www.billerickson.net/?s=wordpress
https://www.billerickson.net/?s=wordpress&post_type=page
https://www.billerickson.net/?s=wordpress&post_type=page,post
https://www.billerickson.net/?s=wordpress&cat=1
https://www.billerickson.net/?s=wordpress&post_type=post&tag=genesis-post
https://www.billerickson.net/?s=wordpress&post_type=post&tag=thesis-post
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!
AJ Clarke says
Seems whenever I try and do a search by page post type only it returns posts as well…I see your site does as well:
https://www.billerickson.net/?s=wordpress&post_type=page
Any thoughts?
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.
Tom Busillo says
Thanks Bill.
Darshan Saroya says
How to exclude one Custom Post Type results via searchform.php?
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.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 thenecho
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.
Then in searchform.php you check if
!empty( $be_excluded_post_type )