WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Limit Search Results to Custom Post Type?

[Resolved] Limit Search Results to Custom Post Type?

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

Viewing 15 replies - 1 through 15 (of 18 total)
  • WPChina

    @wordpresschina

    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.

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

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

    WPChina

    @wordpresschina

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

    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!

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

    Thanks.

    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');

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

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

    greggh3

    @greggh3

    bgajus – what a great nugget!

    iLightning

    @ilightning

    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.

    ralpheweotto

    @ralpheweotto

    @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

    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

    Duy

    @coldzero1120

    This is very helpful, thanks!

    @ralpheweotto you saved my life!

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

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘[Resolved] Limit Search Results to Custom Post Type?’ is closed to new replies.
Skip to toolbar