如何搜索自定义帖子类型?

时间:2012-01-18 作者:osos

我想弄明白,但直到我知道

我有新的自定义帖子类型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 &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 );
                ?>

2 个回复
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() ) :    
... ... ...
希望这有帮助!!!

结束

相关推荐

paginate posts on admin page

我创建了一个插件,可以调用帖子列表并获取外部API数据。我正在使用get\\u posts获取列表(因为我需要将链接数组提供给API查询)无论出于何种原因,分页的任何尝试要么完全失败,要么加载相同的帖子。由于我所做的一切都不起作用,所以现在我删除了所有分页。add_action(\'admin_menu\' , \'blgs_dashboard_snap\'); function blgs_dashboard_snap() { add_dashboard_pa