我想弄明白,但直到我知道
我有新的自定义帖子类型questions
我还有其他类型的帖子job listing
, resumes
等
我想在这里搜索帖子类型的问题只在这里我做了什么
<?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>
和搜索页面
<?php
global $wp_query, $query_string;
$term_heading = \'\';
$find_posts_in = \'\';
$search = get_search_query();
if ($search) :
$term_heading = sprintf( __(\'Searching for Questions “%s” \'), 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 );
?>
SO网友:Eric Holmes
更好的方法是处理主搜索查询@Dipesh的回答在技术上是正确的,但我建议使用pre_get_posts
操作以影响初始查询,而不是生成新查询。更少的工作!
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\' ) );
}
}
SO网友:Dipesh KC
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
添加查询筛选器参数
$search_args[\'post_type\'] = array( \'post\', \'page\', \'movie\', \'book\' ) ) );// all your post types to be included in the search
类似地,根据需要包括尽可能多的过滤器,例如分页
$search_args[\'paged\'] = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
nbsp;
$result = new WP_Query( $search_args );
$total_results = $result ->found_posts;
if ( $result ->have_posts() ) :
... ... ...
希望这有帮助!!!