Dav*_*vey 12 php wordpress search
我想使用wordpress搜索功能,但我希望它只搜索我的博客帖子并从查询中排除我的静态页面.这该怎么做?我并不反对使用插件.
Dea*_*ley 39
答案在这里
http://www.shilling.id.au/2011/06/23/wordpress-search-only-show-post-and-not-pages/
<?php
function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
Run Code Online (Sandbox Code Playgroud)
        如果您未指定不同的帖子类型,此解决方案将使搜索仅检索帖子。如果您在不同搜索字段的隐藏字段中指定自定义帖子类型,则此方法不会干扰。
function searchFilter($query) {
    if ($query->is_search) {
        if ( !isset($query->query_vars['post_type']) ) {
            $query->set('post_type', 'post');
        }
    }
    return $query;
}
add_filter('pre_get_posts','searchFilter');
Run Code Online (Sandbox Code Playgroud)
        这个 wp 论坛页面有很多不同的示例,说明如何执行此操作,具体取决于您要编辑站点的位置(我相信 index.php 或 wp-includes/query.php 是您的选择):
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages