[resolved] Limit Search Results to Custom Post Type? (19 posts)

  1. bgajus
    Posted 6 years ago #

    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?

  2. WPChina
    Posted 6 years ago #

    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.

  3. bgajus
    Posted 6 years ago #

    This has worked for me....
    <input type="hidden" name="post_type" value="post type name" />

  4. teamplaylotto
    Posted 6 years ago #

    bgajus : thanks for that. adding that one little hidden field saved me hours of trouble!!

  5. WPChina
    Posted 6 years ago #

    @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.

  6. adambundy
    Posted 6 years ago #

    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!

  7. madfatter
    Posted 6 years ago #

    I apologize for what might be a stupid questions but where would you put that line of code?


  8. BrenFM
    Posted 5 years ago #

    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;
  9. alanchrishughes
    Posted 5 years ago #

    Thank you. Is there a similar way to simply include custom post types in an overall search?

  10. dapro
    Posted 5 years ago #

    I'm looking for the same answer: How to include custom post types in an overall search?

  11. greggh3
    Posted 5 years ago #

    bgajus - what a great nugget!

  12. iLightning
    Posted 5 years ago #

    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.

    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.

  13. ralpheweotto
    Posted 5 years ago #

    @alanchrishughes @dapro

    I think you guys are looking for:

    Check out the public and publicly_queryable arguements

  14. Cerebral ideas
    Posted 5 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" />

    Obviously, replace post_type_a with your unique post type name and Post Type A with your post type label. Good luck!


  15. Duy
    Posted 5 years ago #

    This is very helpful, thanks!

  16. Cristiano
    Posted 5 years ago #

    @ralpheweotto you saved my life!

    I didnĀ“t realize you could restrict the search by the custom post type arguments... thanks!

  17. alanchrishughes
    Posted 5 years ago #

    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.

  18. SpankMarvin
    Posted 5 years ago #

    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.

  19. Frankie Jarrett
    Posted 5 years ago #


    you're a champ! thank you kindly

Topic Closed

This topic has been closed to new replies.

About this Topic