简体   繁体   中英

WP_query Filter through meta_key and meta_value

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

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" => "")
);`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM