注册帖子状态,从搜索中排除

时间:2020-09-29 作者:Tianbo84

我正在尝试注册一个新的帖子类型,以便在搜索中隐藏帖子,但仍然允许通过url查看帖子。

function reg_ghost_status(){
    register_post_status( \'ghosted\', array(
        \'label\'                     => \'Ghost Post\',
        \'publicly_queryable\'        => false,
        \'exclude_from_search\'       => true,
        \'public\'                    => true, //on false hides everywhere
        \'show_in_admin_all_list\'    => true,
        \'show_in_admin_status_list\' => true,
    ) );
}
add_action( \'init\', \'reg_ghost_status\' );
我无法完成这项工作,尝试了各种组合。似乎因为它是“公共的”,所以无论什么设置,它都会显示在任何地方。因此,我尝试使用pre\\u set\\u查询,但我不知道如何使用exclude而不是include by post\\u status。

function sxcsexclude_ghost_from_search($query) {
        if ( $query->is_single ) {
             $query->set(\'perm\', \'readable\');
             $query->set(\'post_status\', array( \'publish\', \'draft\', \'ghosted\', \'future\' ));
            return $query;
        }
}
add_action( \'pre_get_posts\', \'11111exclude_ghost_from_search\' );
有人能告诉我为什么register\\u post\\u状态不起作用吗。

谢谢

1 个回复
SO网友:wrydere

如果是我,我可能会覆盖您试图隐藏的单个模板上的查询;“鬼影”;发布于。但是,我可以看到,在某些情况下,重写主查询会更好。

怎么样:

function public_query_published_only($query) {
  if ( !$query->is_single() && !current_user_can(\'manage_options\') && !is_admin() ) {
        $query->set(\'post_status\', array(\'publish\') );
        return $query;
    }
}
add_action( \'pre_get_posts\', \'public_query_published_only\' );
因此:如果查询不是针对单个帖子,并且用户不是登录用户,并且查询不是来自管理界面,则仅显示状态为“的帖子”;发布;。(当然,您还可以添加您认为可以查询的任何其他状态。)

由于没有针对post状态的排除过滤器,因此这是我所能想到的最好的方法,无需求助于SQL。

相关推荐

Get posts for each user

对于学生项目,我使用WordPress和木材(树枝)+ACF在这个项目中,我创建了3种自定义帖子类型:dissertation, subject-imposed 和subject-free每个学生只能按自定义帖子类型创建一篇帖子(我为此创建了一个限制)。但现在我想显示一个列表,列出每个学生的名字和他们的3篇帖子。这样的列表:Nicolas Mapple<自定义职位类型的标题dissertation + 自定义帖子类型名称+图像(ACF字段)自定义帖子类型的标题subject-imposed + 自定