Wordpress循环过滤元 [英] Wordpress loop to filter out by meta

查看:193
本文介绍了Wordpress循环过滤元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ p>

我试图隐藏包含特定元数据的帖子,在这种情况下meta_name =smartPrivate。

Thanks in advance Rob

有谁知道我可以如何隐藏这些帖子出现在所有循环中,但不会影响菜单(谁知道还有什么...)

在此先感谢
Rob

function hide_some_posts( $query ) { if (!is_user_logged_in()) { $query->set( 'meta_query', array( array( 'key' => 'smartPrivate', 'value' => 'smartPrivate_loggedIn', 'compare' => '!=' ), array( 'key' => 'smartPrivate', 'value' => 'smartPrivate_loggedInMentors', 'compare' => '!=' ) )); } return $query; } add_filter( 'pre_get_posts', 'hide_some_posts' );


推荐答案

所以你的问题是,主要查询,如果我正确理解你的情况。这几乎是为什么存在 is_main_query 。所以试试这个:

So your problem is that it affects other queries than the main query, if I understand your situation correctly. This is pretty much why is_main_query exists. So try this:

function hide_some_posts( $query ) {


    if (!is_user_logged_in() && $query->is_main_query() ) {

        $query->set( 'meta_query', array(

            array(
                  'key' => 'smartPrivate',
                  'value' => 'smartPrivate_loggedIn',
                  'compare' => '!='
            ),
            array(
                  'key' => 'smartPrivate',
                  'value' => 'smartPrivate_loggedInMentors',
                  'compare' => '!='
            )

        ));
    }

  return $query;
}
add_filter( 'pre_get_posts', 'hide_some_posts' );

这篇关于Wordpress循环过滤元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆