Meta_key和Meta_Value不在一起工作

时间:2016-07-26 作者:Jonathan Lafleur

我正在尝试过滤循环以查找具有meta_key 具有特定的meta_value. 我看了一下抄本,尝试了以下方法,但没有成功:

// No results
$args = array(
    \'post_type\' => \'cqpp_interventions\',
    \'posts_per_page\' => \'-1\',
    \'meta_query\' => array(
            \'relation\' => \'OR\',
            array(
                \'meta_key\' => \'priority\',
                /* Tried this too
                \'meta_value\' => \'80\',
                \'compare\' => \'=\'
                */
                \'meta_value\' => array(\'80\'),
                \'compare\' => \'IN\' )
    )
);

// No results
$args = array(
    \'post_type\' => \'cqpp_interventions\',
    \'posts_per_page\' => \'-1\',
    \'meta_key\' => \'priority\',
    \'meta_value\' => 80
);

// This list me all cqpp_interventions and I can confirm that I have some with meta_value set to 80
$args = array(
    \'post_type\' => \'cqpp_interventions\',
    \'posts_per_page\' => \'-1\',
    \'meta_key\' => \'priority\'
);


$cqpp_posts = get_posts( $args );
以下是我如何在循环内进行验证:

$priority = get_post_meta( get_the_ID(), \'priority\');
echo \'<pre>\';
var_dump($priority);
echo \'</pre>\';
其结果是:

search.php:16:
array (size=1)
    0 => 
        array (size=1)
            0 => string \'80\' (length=2)

search.php:16:
array (size=1)
    0 => 
        array (size=2)
        0 => string \'80\' (length=2)
        1 => string \'91\' (length=2)
我能做些什么来解决这个问题?

2 个回复
SO网友:Chin Leung

您可以尝试以下操作:

$args = array(
    \'post_type\' => \'cqpp_interventions\',
    \'posts_per_page\' => \'-1\',
    \'meta_query\' => array(
        array(
            \'key\' => \'priority\',
            \'value\' => \'80\'
        )
    )
);
您的查询的真正问题是因为您通过了meta_keymeta_value. 但是meta_query 参数应具有键keyvalue 相反

这也会起作用:

\'key\' => \'priority\',
\'value\' => array(\'80\')

SO网友:Aftab

从您的第一个$参数引用

$args = array(
    \'post_type\' => \'cqpp_interventions\',
    \'posts_per_page\' => \'-1\',
    \'meta_query\' => array(
            \'relation\' => \'OR\',
            array(
                \'meta_key\' => \'priority\',
                /* Tried this too
                \'meta_value\' => \'80\',
                \'compare\' => \'=\'
                */
                \'meta_value\' => array(\'80\'),
                \'compare\' => \'IN\' )
    )
);
您已添加\'relation\' => \'OR\', 这是不需要的。这个relation 如果要查询多个元值,则需要添加。如果需要特定值,也不需要比较,因为默认情况下是使用=.

更新后的$参数将为

$args = array(
    \'post_type\' => \'cqpp_interventions\',
    \'posts_per_page\' => \'-1\',
    \'meta_query\' => array(
            array(
                \'meta_key\' => \'priority\',
                \'meta_value\' => \'80\', // since 80 is string
             )
    )
);

// try either of below.
$myPost = new WP_Query( $args ); // fetch post and managed in objects
$myPost = get_posts( $args ); // fetch post and store in array
希望这能帮到你。

谢谢

相关推荐

如何在wp-Query中比较不同的时间戳以获取事件自定义帖子类型?

我有一个带有自定义元数据库的自定义帖子类型(as seen on wptheming.com) 并且希望进行查询以显示即将发生的事件,例如在侧边栏中,过去的日期被忽略。但我需要一个当前时间的值来和事件开始时间进行比较。现在我从current_time(\'mysql\') 看起来是这样的:2012-02-16 13:15:31元的开始时间如下所示:201108121100如何使这两个日期具有可比性?我可以在查询中执行吗?或者是否有其他解决方案?以下是我到目前为止的查询(值留空,时间戳回显):<?ph