首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress查询,根据多个键检查一个值

Wordpress查询,根据多个键检查一个值
EN

Stack Overflow用户
提问于 2015-12-31 05:14:17
回答 1查看 723关注 0票数 0

我的Wordpress安装程序具有发布自定义post类型,并有10个自定义字段,每个字段对应于可能对该出版物作出贡献的作者。每个帖子可以有不同的作者在这些领域或相同的作者,但以不同的顺序。我正在使用Wordpress的WP_Query搜索符合我的标准的帖子。我需要对照所有的自定义字段检查一个作者的名字,看看它是否匹配其中的任何一个。我试着用下面的代码来做这件事,但是它没有产生任何结果。任何帮助都将不胜感激!

代码语言:javascript
复制
$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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-31 05:46:00

我为post author查询添加了或relation,尝试更改参数数组如下:

代码语言:javascript
复制
$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 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34541367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档