Wordpress meta_query将3个元值与相同的元键进行比较

时间:2014-03-27 22:50:52

标签: wordpress plugins

我想获取所有包含meta_value Book,Study,Art的帖子。但是当我在meta_query系统上放置3个数组时,并没有返回任何内容,但是如果我把2个数组放在它的工作中

// this working
     $args = array(
    'numberposts' => 10,
    'post_type' => 'content',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'media_type',
            'value' => 'Book',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'media_type',
            'value' => 'Study',
            'compare' => 'LIKE'
        )       
    )
);


//this now working
 $args = array(
    'numberposts' => 10,
    'post_type' => 'content',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'media_type',
            'value' => 'Book',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'media_type',
            'value' => 'Study',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'media_type',
            'value' => 'Art',
            'compare' => 'LIKE'
        )
    )
);

1 个答案:

答案 0 :(得分:1)

听起来您正在搜索确切的元值,所以您尝试了:

$args = array(
     'posts_per_page' => 10,
     'post_type'      => 'content',
     'meta_query'     => array(
            array(
                'key'     => 'media_type',
                'value'   => array( 'Art', 'Book','Study' ),
                'compare' => 'IN'
            ),
      ),
);

作为您的查询参数?

Sidenote :参数numberposts有效,但posts_per_page现在更常用WP_Query()