alanchrishughes
Member
Posted 1 year ago #
Is there a way to build a search bar for a custom post type? WordPress.org's codex has this as the code for the search bar
<form role="search" method="get" id="searchform" action="<?php bloginfo('url'); ?>">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
I tried replacing the bloginfo('url') with the slug url for my custom post types, but nothing. Any ideas?
alanchrishughes
Member
Posted 1 year ago #
mamouneyya
Member
Posted 1 year ago #
Did you get this working?
For my case, I need a drop-down menu to restrict the search results to one of my custom posts types. Could you help with that, please?
alanchrishughes
Member
Posted 1 year ago #
Sorry man, I have only been able to get this working for one post type per search bar.
mamouneyya
Member
Posted 1 year ago #
OK. Never mind. I'll start another thread.
alanchrishughes
Member
Posted 1 year ago #
mamouneyya
Member
Posted 1 year ago #
Nope :( . No one answered!
alanchrishughes
Member
Posted 1 year ago #
That sucks. I'm looking for a way to include two post types in a search now.
Cerebral ideas
Member
Posted 1 year ago #
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
alanchrishughes
Member
Posted 1 year ago #
Thanks Justin, is there a way to do that automatically with just one specific post type?
Replace the select field with a hidden one that explicitly sets the type.
<input type="hidden" name="post_type" value="your_post_type" />
alanchrishughes
Member
Posted 1 year ago #
alanchrishughes
Member
Posted 1 year ago #
Is it possible to do that for two (or more) specific post types? I've tried value="posttypeone, posttypetwo" but that didn't work.
Cerebral ideas
Member
Posted 1 year ago #
Would't that be ...
<input type="hidden" name="post_type_one" value="posttypeone" />
<input type="hidden" name="post_type_two" value="posttypetwo" />
From what I understand, there's no need to stuff multiple values into one hidden input, just create multiple hidden inputs.
alanchrishughes
Member
Posted 1 year ago #
Thanks Justin, but that seems to ignore what ever the first input value is and just works with the last.
Cerebral ideas
Member
Posted 1 year ago #
Yeah, looking back at the advice I gave you, it's not right for a couple of reasons. Now that I am actually testing it, I can't figure out how to do it either. I'll report back if I figure it out.
alanchrishughes
Member
Posted 1 year ago #
Thanks again, I appreciate any help you can share.
Cerebral ideas
Member
Posted 1 year ago #
Hmm, this is seeming to be more complicated than I originally thought. Though this discussion may help you along:
http://wordpress.org/support/topic/how-to-include-custom-post-types-in-search-results-and-archives?replies=15
I don't have too much free time to play with this, but if I do, I'll keep in touch.
dallasclark
Member
Posted 9 months ago #
To search across multiple post types, use the following
<input type="hidden" name="post_type[]" value="your_post_type" />
<input type="hidden" name="post_type[]" value="your_post_type" />