【问题标题】:WP_query Filter through meta_key and meta_valueWP_query 通过 meta_key 和 meta_value 过滤
【发布时间】:2023-04-04 15:41:02
【问题描述】:

到目前为止,这是通过过滤并仅显示“会话”“秋季”项目来工作的。

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

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

    【讨论】:

    • 那和我刚刚尝试的代码不起作用。我在代码中做错了吗?
    猜你喜欢
    • 2013-04-08
    • 2016-03-22
    • 2019-11-06
    • 2018-02-16
    • 2020-04-21
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多