How to make search for the custom post type?

i tried to figure this out but i can’t until know

i have new custom post type of questions and i have other post types also like job listing, resumes etc

Read More

what i want here to make search for the post type questions only
here what i done

            <?php global $app_abbr, $header_search; $header_search = true; ?>
            <?php get_header(); ?>



                <form action="<?php bloginfo('url'); ?>/" method="get" id="searchform">

                    <div class="search-wrap">

                        <div>
                            <input type="hidden" name="questions_search" value="true" />
                            <input type="hidden" name="post_type" value="questions" />              
                            <input type="text" id="search" title="" name="s" class="text" placeholder="<?php _e('All Questions and answers'); ?>" value="<?php if (isset($_GET['s'])) echo get_search_query(); ?>" />
                            <label for="search"><button type="submit" title="<?php _e('Go'); ?>" class="submit"><?php _e('Go'); ?></button></label>

                        </div>

                    </div><!-- end search-wrap -->

                </form>

and the search page

                <?php
                global $wp_query, $query_string;

                $term_heading = '';
                $find_posts_in = '';

                $search = get_search_query();

                if ($search) :
                    $term_heading = sprintf( __('Searching  for Questions &ldquo;%s&rdquo; '), get_search_query());
                else :
                    $term_heading = __('Searching  ');
                endif;


                if (is_array($find_posts_in)) :
                    $args = array_merge( $wp_query->query,
                        array(
                            'post_type' => 'questions',
                            'post__in' => $find_posts_in
                        )
                    );
                else :
                     $args = array_merge( $wp_query->query,
                        array(
                            'post_type' => 'questions'
                        )
                    );
                endif;
                query_posts( $args );
                ?>

Related posts

Leave a Reply

2 comments

  1. A better approach is to address the main search query. @Dipesh’s answer is technically correct, but I would suggest using the pre_get_posts action instead to affect the intial query, rather than generating a new one. Much less work!

    add_action( 'pre_get_posts', 'se39294_search_pre_get_posts' );
    
    function se39294_search_pre_get_posts( $query ) {
        if ( $query->is_main_query() && is_search() ) {
            $query->set( 'post_type', array( 'page', 'post', 'questions' ) );
        }
    }
    
  2. global $query_string;    
    $query_args = explode("&", $query_string);
    $search_args = array();
    
    //including the default search queries 
    foreach($query_args as $key => $string) {
        $query_split = explode("=", $string);
        $search_args[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
    

    add your query filter arguments

     $search_args['post_type'] =  array( 'post', 'page', 'movie', 'book' ) ) );// all your post types to be included in the search
    

    similarly include as many filters as you need eg for paging

    $search_args['paged'] = (get_query_var('paged')) ? get_query_var('paged') : 1;
    

     

    $result = new WP_Query( $search_args   );
    
    $total_results     =             $result ->found_posts;
    
    if ( $result ->have_posts() ) :    
    ... ... ...
    

    Hope this helps!!!