Wordpress查询,针对多个键检查值

时间:2015-12-31 05:14:18

标签: wordpress wp-query

我的Wordpress设置有一个发布自定义帖子类型,并且有10个自定义字段,每个字段对应于可能为该出版物做出贡献的作者。每个帖子可以在这些字段或同一作者中以不同的顺序具有不同的作者。我正在使用Wordpress' WP_Query来搜索符合我条件的帖子。 我需要针对所有自定义字段检查单个作者名称,以查看它是否与其中任何一个匹配。我尝试使用以下代码执行此操作,但它不会产生任何结果。任何帮助将不胜感激!

$args = array(
    'post_type' => 'publication',
    'post_status' => 'publish',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'year_published',
            'value' => $selected_pub_year,
            'compare' => '=',
        ),
        array(
            'key' => array(
                'author_0_name',
                'author_1_name',
                'author_2_name',
                'author_3_name',
                'author_4_name',
                'author_5_name',
                'author_6_name',
                'author_7_name',
                'author_8_name',
                'author_9_name',
            ),
            'value' => $selected_pub_author,
            'compare' => '=',
        ),
    ),
);

// The above meta-query may be modified through the use of this:
// echo "<pre>".print_r($args[meta_query][relation])."</pre>";

// The Query
$the_query = new WP_Query($args);

1 个答案:

答案 0 :(得分:1)

我为帖子作者查询添加了 OR关系,尝试更改你的参数数组,如下所示,

$args = array(
    'post_type'  => 'publication',
    'post_status' => 'publish',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'year_published',
            'value'   => $selected_pub_year,
            'compare' => '=',
        ),
        array(
            'relation' => 'OR',
             array(
                'key'     => 'author_0_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_1_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_2_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_3_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_4_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_5_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_6_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_7_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_8_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
            array(
                'key'     => 'author_9_name',
                'value'   => $selected_pub_author,
                'compare' => '=',
            ),
        ),
    ),
);
$the_query = new WP_Query( $args );