如何组合Meta_Query查询?

时间:2012-11-16 作者:Hinnerk Altenburg

我正在尝试构建组合meta\\u查询,但它们不起作用。有什么想法可以实现下面这样的查询吗?

谢谢Hinnerk

Array
(
    [relation] => AND
    [0] => Array
        (
            [key] => price
            [value] => 1000
            [type] => numeric
            [compare] => <=
        )

    [1] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [key] => roomcount
                    [value] => 3
                    [type] => numeric
                    [compare] => >=
                )

            [1] => Array
                (
                    [key] => bathroomcount
                    [value] => 2
                    [type] => numeric
                    [compare] => >=
                )

        )

)

1 个回复
SO网友:Chip Bennett

使用构造的数组,我认为这是正确的语法:

$meta_query_args = array(
    \'meta_key\' => \'price\',
    \'meta_value_num\' => 1000,
    \'meta_compare\' => \'<=\',
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'key\' => \'roomcount\',
            \'value\' => 3,
            \'type\' => \'numeric\',
            \'compare\' => \'>=\'
        ),
        array(
            \'key\' => \'bathroomcount\',
            \'value\' => 2,
            \'type\' => \'numeric\',
            \'compare\' => \'>=\'
        )
    )
);

$realestate_meta_query = new WP_Query( $meta_query_args );
您也可以尝试嵌套meta_query 还有一个级别(虽然我不知道这是否有效):

$meta_query_args = array(
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\' => \'price\',
            \'value\' => 1000,
            \'type\' => \'numeric\',
            \'compare\' => \'<=\'
        ),
        array(
            \'relation\' => \'OR\',
            array(
                \'key\' => \'roomcount\',
                \'value\' => 3,
                \'type\' => \'numeric\',
                \'compare\' => \'>=\'
            ),
            array(
                \'key\' => \'bathroomcount\',
                \'value\' => 2,
                \'type\' => \'numeric\',
                \'compare\' => \'>=\'
            )
        )
    )
);

$realestate_meta_query = new WP_Query( $meta_query_args );

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post