WP_query Filter through meta_key and meta_value

2019-08-07 15:56发布

So far this is working by filtering through and only displaying the 'sessions' 'Fall' items.

<?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(); 
?>

But I want it to also filter and only display items that are in the "fall" and at "Monon Community Center"

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

How can I accomplish this?

I also tried this and it did not work

                        $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(); 

标签: php wordpress
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-07 16:43

Under "Custom Field Parameters" on http://codex.wordpress.org/Class_Reference/WP_Query you can pass a meta_query that is an array:

$meta_query = array(
    array("key" => "value", "value" => "value2", "compare" => ""),
    array("key" => "value3", "value" => "value4", "compare" => "")
);`
查看更多
登录 后发表回答