WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Search Bar (19 posts)

  1. alanchrishughes
    Member
    Posted 3 years 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?

  2. alanchrishughes
    Member
    Posted 3 years ago #

  3. mamouneyya
    Member
    Posted 3 years 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?

  4. alanchrishughes
    Member
    Posted 3 years ago #

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

  5. mamouneyya
    Member
    Posted 3 years ago #

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

  6. alanchrishughes
    Member
    Posted 3 years ago #

    Any luck?

  7. mamouneyya
    Member
    Posted 3 years ago #

    Nope :( . No one answered!

  8. alanchrishughes
    Member
    Posted 3 years ago #

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

  9. Cerebral ideas
    Member
    Posted 3 years 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

  10. alanchrishughes
    Member
    Posted 3 years ago #

    Thanks Justin, is there a way to do that automatically with just one specific post type?

  11. Mark / t31os
    Moderator
    Posted 3 years ago #

    Replace the select field with a hidden one that explicitly sets the type.

    <input type="hidden" name="post_type" value="your_post_type" />
  12. alanchrishughes
    Member
    Posted 3 years ago #

    Thanks Mark.

  13. alanchrishughes
    Member
    Posted 3 years 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.

  14. Cerebral ideas
    Member
    Posted 3 years 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.

  15. alanchrishughes
    Member
    Posted 3 years ago #

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

  16. Cerebral ideas
    Member
    Posted 3 years 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.

  17. alanchrishughes
    Member
    Posted 3 years ago #

    Thanks again, I appreciate any help you can share.

  18. Cerebral ideas
    Member
    Posted 3 years 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.

  19. dallasclark
    Member
    Posted 3 years 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" />

Topic Closed

This topic has been closed to new replies.

About this Topic