我正在为一些自定义帖子类型构建一个搜索表单。问题是,每次搜索都会产生相同的结果,即所有自定义类型的帖子。请告知:
代码:
搜索表单:
<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">
</form>
搜索模板:
<div class="row">
<?php
$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()) {
the_post_thumbnail();
} else { ?>
<img src="http://placehold.it/360x220" alt="">
<?php } ?>
</a>
</div>
<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>
</p>
</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>
</div>
</div>
</div>
<?php endif;
wp_reset_postdata();
?>
最合适的回答,由SO网友:James Barrett 整理而成
看起来您没有在WP\\U查询参数中传递搜索查询。要在自定义搜索页面上执行此操作,请尝试以下操作:
替换此:
$the_query = new WP_Query(\'post_type=darpe-entries\');
使用此选项:
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);