WP_query通过meta_key和meta_value过滤

时间:2013-05-20 20:29:18

标签: php wordpress

到目前为止,这是通过过滤并仅显示'会话''秋季'项目来实现的。

<?php       
    $the_query = new WP_Query( array( 
            'post_type' => 'classes',
            'meta_key' => 'sessions'
            'meta_value' => 'Fall',
            'posts_per_page' => -1
            ));

    while ($the_query->have_posts()) :
    $the_query->the_post(); 
?>

但我希望它也能过滤并只显示“秋天”和“Monon社区中心”中的项目

'meta_key' => 'location_select',
'meta_value' => 'Monon Community Center',

我该如何做到这一点?

我也尝试了这个并且它不起作用

                        $the_query = new WP_Query( array( 
            'post_type' => 'classes',
            'meta_query' => array(
                        'relation' => 'AND',
                        array(
                                'meta_key' => 'location_select',
                                'meta_value' => 'Monon Community Center',
                                'compare' => '='),
                        array(
                                'meta_key' => 'sessions',
                                'meta_value' => 'fall',
                                'compare' => '='),
                        'posts_per_page' => -1
                )
                ));
        while ($the_query->have_posts()) :
        $the_query->the_post(); 

1 个答案:

答案 0 :(得分:0)

http://codex.wordpress.org/Class_Reference/WP_Query的“自定义字段参数”下,您可以传递一个数组的元查询:

$meta_query = array(
    array("key" => "value", "value" => "value2", "compare" => ""),
    array("key" => "value3", "value" => "value4", "compare" => "")
);`