I'm working with custom post types and taxonomies. Wondering if anyone knows of a way to create a secondary search that would limit results to only posts from that custom post type?
I'm working with custom post types and taxonomies. Wondering if anyone knows of a way to create a secondary search that would limit results to only posts from that custom post type?
I am seeking this too. I would like to be able to:
1) Search only specific custom post types. For example if I have Books and Food, I only want to be able to search for Books posts.
2) Conduct searches on fields within the custom post types. For example, if I have posts under Books, I want to have allow users to search on a dropdown for the language the book and/or enter text for an author's name and/or enter text in another field for the year of the book's publishing... then all/any of the search choices will be used to generate search results.
This has worked for me....
<input type="hidden" name="post_type" value="post type name" />
bgajus : thanks for that. adding that one little hidden field saved me hours of trouble!!
@bgajus: Yes, this is great and works well!
However is there a means by which I can search within a specific field? My goal is to create a listings website and natively use WordPress' new features to help search within a field.
This doesnt seem to work for me- the search ignored the passed post_type data and searches in all types. Any tips or sample code from those who had success with this? Thanks!
I apologize for what might be a stupid questions but where would you put that line of code?
Thanks.
I've used the following. In it's raw state, all you need to do is put &type=post_type (i.e. &type=movies) on the end of the URL. You could probably do this with a drop down/radio buttons on your search form.
function mySearchFilter($query) {
$post_type = $_GET['type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');Thank you. Is there a similar way to simply include custom post types in an overall search?
I'm looking for the same answer: How to include custom post types in an overall search?
bgajus - what a great nugget!
I was having some trouble getting bgajus's solution to work. After some poking around I found that I needed to have this at the top of my search.php template file.
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = $query_split[1];
} // foreach
$search = new WP_Query($search_query);
?>
Then follow the instructions here when you set up your search results loop.
@alanchrishughes @dapro
I think you guys are looking for:
http://codex.wordpress.org/Function_Reference/register_post_type#Parameters
Check out the public and publicly_queryable arguements
Just in case anyone is looking for a way to create a search form that provides selectable drop-down list of their custom post types, here is what worked for me:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value="Enter keywords ..." onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/><br />
<select name="post_type">
<option value="">Choose Category:</option>
<option value="">All Categories</option>
<option value="post_type_a">Post Type A</option>
<option value="post_type_b">Post Type B</option>
<option value="post_type_c">Post Type C</option>
</select><br />
<input type="submit" id="searchsubmit" value="Search Help" />
</form>
Obviously, replace post_type_a with your unique post type name and Post Type A with your post type label. Good luck!
Justin
http://www.cerebralideas.com
This is very helpful, thanks!
@ralpheweotto you saved my life!
I didn“t realize you could restrict the search by the custom post type arguments... thanks!
How would you go about searching only two specific custom post types?
@bgajus code worked easily for one post type
<input type="hidden" name="post_type" value="one" />
but then if I tried to search two specific types
`<input type="hidden" name="post_type" value="one, two" />'
did not work for either.
Wow, what a useful piece of info. Thank you!
Regarding multiple posts types, it seems that the WP_Query object can handle the 'post_type' parameter as an array. Therefore, it seems logical to me that if you were to pass a form array for post_type input name, it would work. I'm not in a position to test this right now, but would the following work?:
<input type="hidden" name="post_type[]" value="post_type_one" />
<input type="hidden" name="post_type[]" value="post_type_two" />
<input type="hidden" name="post_type[]" value="post_type_three" />
Hope that's useful.
@bgajus
you're a champ! thank you kindly
This topic has been closed to new replies.