WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. bgajus
    Member
    Posted 4 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
    Member
    Posted 4 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
    Member
    Posted 4 years ago #

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

  4. teamplaylotto
    Member
    Posted 4 years ago #

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

  5. WPChina
    Member
    Posted 4 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
    Member
    Posted 3 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
    Member
    Posted 3 years ago #

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

    Thanks.

  8. BrenFM
    Member
    Posted 3 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;
    };
    
    add_filter('pre_get_posts','mySearchFilter');
  9. alanchrishughes
    Member
    Posted 3 years ago #

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

  10. dapro
    Member
    Posted 3 years ago #

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

  11. greggh3
    Member
    Posted 3 years ago #

    bgajus - what a great nugget!

  12. iLightning
    Member
    Posted 3 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.

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

  13. ralpheweotto
    Member
    Posted 3 years ago #

    @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

  14. 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
    http://www.cerebralideas.com

  15. Duy
    Member
    Posted 3 years ago #

    This is very helpful, thanks!

  16. Cristiano
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 years ago #

    @bgajus

    you're a champ! thank you kindly

Topic Closed

This topic has been closed to new replies.

About this Topic