Support » Plugins and Hacks » Custom Post Type Search Bar

  • 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?

Viewing 15 replies - 1 through 15 (of 18 total)
  • 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?

    Sorry man, I have only been able to get this working for one post type per search bar.

    OK. Never mind. I’ll start another thread.

    Any luck?

    Nope 🙁 . No one answered!

    That sucks. I’m looking for a way to include two post types in a search now.

    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

    (@alanchrishughes)

    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

    (@alanchrishughes)

    Thanks Mark.

    alanchrishughes

    (@alanchrishughes)

    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

    (@cerebral-ideas)

    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

    (@alanchrishughes)

    Thanks Justin, but that seems to ignore what ever the first input value is and just works with the last.

    Cerebral ideas

    (@cerebral-ideas)

    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.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Custom Post Type Search Bar’ is closed to new replies.