如何查询具有多个特定标签的帖子?

时间:2015-12-05 17:09:00

标签: php wordpress wp-query

我想查询同时包含$apple$orange标记的帖子。
使用tag__in会返回包含$apple$orange个标记的帖子列表。
但是我想要那些同时拥有它们的帖子。

这是我的代码:

$apple = 276; // tag id of Apple
$orange = 29; // tag id of orange

$args  = array(
    'post_type'     => array(
                        'post',
                        'review',
                        'cardguide'
                    ),
    'taxonomy'      => 'post_tag',
    'posts_per_page'=> - 1,
    'tag__in'       => array(
                        $apple,
                        $orange
                    ),
    'orderby'       => 'id',
    'order'         => 'DESC',

    'meta_key'      => 'event_end_date',
    'meta_query'    => array(
        'relation'=> 'AND',
        array(
            'key'    => 'event_start_date',
            'value'  => $today,
            'compare'=> '<=',
            'type'   => 'DATE'
        ),
        array(
            'key'    => 'event_end_date',
            'value'  => $today,
            'compare'=> '>=',
            'type'   => 'DATE'
        )
    )

);

由于我已经有meta_query,所以我很困惑如何修改它以获得预期的输出。使用=运算符为这些标记添加新数组进行比较并不会有效。

'meta_query'    => array(
    'relation'=> 'AND',
    array(
        'key'    => 'event_start_date',
        'value'  => $today,
        'compare'=> '<=',
        'type'   => 'DATE'
    ),
    array(
        'key'    => 'event_end_date',
        'value'  => $today,
        'compare'=> '>=',
        'type'   => 'DATE'
    ),
    array(
        'key'    => 'tag_id',
        'value'  => $apple,
        'compare'=> '=',
    ),
    array(
        'key'    => 'tag_id',
        'value'  => $orange,
        'compare'=> '=',
    ),
)

2 个答案:

答案 0 :(得分:1)

我认为您应该使用税务查询而不是元查询。

所以你的代码将是:

$taxQuery = array('relation' => 'AND');

array_push($taxQuery,array(
    'taxonomy' => 'post_tag',
    'field' => 'id',
    'terms' => $apple,
));
array_push($taxQuery,array(
    'taxonomy' => 'post_tag',
    'field' => 'id',
    'terms' => $orange,
));


// And add in your args like tax-query
$args  = array(
'post_type'     => array(
                    'post',
                    'review',
                    'cardguide'
                ),

'posts_per_page'=> - 1,

'orderby'       => 'id',
'order'         => 'DESC',

'meta_key'      => 'event_end_date',
'meta_query'    => array(
    'relation'=> 'AND',
    array(
        'key'    => 'event_start_date',
        'value'  => $today,
        'compare'=> '<=',
        'type'   => 'DATE'
    ),
    array(
        'key'    => 'event_end_date',
        'value'  => $today,
        'compare'=> '>=',
        'type'   => 'DATE'
    )
),
'tax_query' => $taxQuery


);

答案 1 :(得分:0)

你为什么不尝试这样的事情:

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}
alert('age: ' + getAge("1980/08/10"));