I'm working on building a search form for some custom post type. Problem is, the search yields the same results every time, which is all posts of that custom type. Please advice:


Search form:

<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >  
  <input type="text" name="s" placeholder="Search DARPE">
    <input type="hidden" name="post_type" value="darpe-entries"> 
    <input type="submit" alt="Search" value="Search">


Search template:

 <div  class="row">
$the_query = new WP_Query('post_type=darpe-entries');

 if($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>

                    <div class="post-item col-md-6 col-sm-6">
                        <div <?php post_class();?>>
                            <div class="blog-grid-item" id="post-<?php the_ID(); ?>">
                                <div class="blog-grid-thumb">
                                    <span class="cat-blog"><?php the_category(', ') ?></span>
                                    <a href="<?php the_permalink(); ?>">
                                        <?php if (has_post_thumbnail()) {

                                            } else { ?>
                                                <img src="http://placehold.it/360x220" alt="">

                                        <?php } ?>
                                <div class="box-content-inner">
                                    <h4 class="blog-grid-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                                    <p class="blog-grid-meta small-text">
                                        <span><?php the_time('F j, Y'); ?></span>
                                        <?php _e(' With ', CORE_THEME_NAME ); ?>
                                        <span><?php comments_popup_link('No comments', '1 comment', '% comments', 'comments-link', 'Comments are closed'); ?></span>
                                </div> <!-- /.box-content-inner -->
                            </div> <!-- /.blog-grid-item -->
                        </div> <!-- /.post-class -->
                    </div> <!-- /.col-md-6 -->
                    <?php endwhile; else : ?>
                            <div class="col-md-12">
                                <div class="widget-main">
                                    <div class="widget-inner">
                                        <p><?php _e( 'No posts found.', CORE_THEME_NAME ); ?></p>

                    <?php endif;
  • Do not use a custom query on your search template. Use pre_get_posts to correctly alter the main query according to your needs Jun 21, 2015 at 15:40

1 Answer 1


It doesn't look like you're passing your search query in your WP_Query args. To do this on a custom search page try the following:

Replace this:

$the_query = new WP_Query('post_type=darpe-entries');

With this:

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]] = urldecode($query_split[1]);

$search_query['post_type'] = 'darpe-entries'; // your custom post type

$the_query = new WP_Query($search_query);
  • Don't use a custom query to alter the main query on the search page, use pre_get_posts instead to alter the main query :-) Jun 21, 2015 at 15:36
  • Yep @PieterGoosen is right. I had my blinkers on :) Jun 21, 2015 at 16:46
  • Thanks to both of you. It's working and I will try with pre_get_posts, too. Can you suggest a way to show results before user do his first search? Jun 21, 2015 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.