• Hello.

    I created a custom post type called portfolio to keep my portfolio itens, i created also a Page called Portfolio to get all custom post type from portfolio and everything works properly.

    So i decided to create a search because I have a good amount of items and will be more ease to search for something. Searching on Google i found a topic that says to put a hidden field on my search form.

    <input type=”hidden” name=”post_type” value=”portfolio” />

    Where portfolio is the name of my custom post type. When i try to search for something, the address looks like this:

    https://www.mysite.com/?s=search word&post_type=portfolio

    Theory was to work, but returns nothing, and if i take off the ?s=search word and just let ?post_type=portfolio the page return all my custom post type, in other words, it was to be working.

    I used Custom Post Types UI plugin to create my custom post type and its set public and query var to true.

    Someone could tell me what can I do to get this problem fix?

    Thanks!

    []s

Viewing 5 replies - 1 through 5 (of 5 total)
  • did you set ‘exclude_from_search’ => false ?

    -Brad
    MyCustomerTools.com

    I have the same or similar problem. I have two custom post types “news” and “games”, both are registered similarly with ‘public’ => true and I’d like both of them to be included in the main search. Problem is, news items show up, but games do not. Drives me nuts.

    Here are the two registries:

    register_post_type( 'news', array(
    	'labels' => array(
    		'name' => __('Press'),
    		'singular_name' => __('news item'),
    		'add_new_item' => __('Add News Item'),
    		'edit_item' => __('Edit News Item'),
    		'view_item' => __( 'View News Item'),
    	),
    	'supports' => array('title', 'custom-fields'),
    	'public' => true,
    	'rewrite' => array('slug' => 'news', 'with_front' => false),
    	'menu_position' => 9,
    ));
    register_post_type( 'games', array(
    	'labels' => array(
    		'name' => __('Games'),
    		'singular_name' => __('game'),
    		'add_new_item' => __('Add Game'),
    		'edit_item' => __('Edit Game'),
    		'view_item' => __( 'View Game'),
    	),
    	'supports' => array('title', 'editor', 'thumbnail'),
    	'public' => true,
    	'rewrite' => array('slug' => 'games', 'with_front' => false),
    	'menu_position' => 5,
    ));

    Even if I add 'exclude_from_search' => false, to the game registration, just to be sure, it doesn’t change anything.

    If I add <input type="hidden" name="post_type" value="games" /> to my search form, I can search for games, but then ONLY games show up. Without the hidden field everything other than games shows up – regular posts, pages, even news – but no games.

    Any ideas?

    Any help?

    Still couldn’t figure this out. Could it be a WordPress bug?

    I fixed this.

    Open file search.php and after line get_header() add so code:

    <?php
    /**
     * Display custom post type
     */
    add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
    	if ( is_search() && false == $query->query_vars['suppress_filters'] )
    		$query->set( 'post_type', array( 'post', 'gallery', 'work' ) ); // Set post types
    	return $query;
    }
    
    /**
     * !!! Without lines bellow WP display warnings !!!
     */
    global $query_string;
    query_posts($query_string); // !!! its very important line !!!
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Searching for Custom Post Type’ is closed to new replies.