WP query_posts()OR运算符

时间:2014-08-22 14:06:22

标签: wordpress

我需要根据meta_keys获取帖子。

目前我有这段代码

global $wp_query;
$args = array_merge( $wp_query->query_vars, 
        array( 
                'meta_key' => 'conference_start_date',
                'orderby' => 'meta_value'
        ) 
);

cakePHP中,会添加OR条件,如此

'conditions' =>  array (
      'OR' => array(
           'meta_key' => 'conference_start_date',
           'meta_key' => 'event_start_date'
       ),
       'orderby' => 'meta_value'

)

如何使用WP query_post执行此操作?即。使用OR

添加'meta_key' => 'event_start_date'

[答案]

'meta_query' => array(
        'relation' => 'OR',
                array(
                    'key'     => 'conference_start_date',
                    'value'   => $now,
                    'compare' => '>='
                ),
                array(
                    'key' => 'event_start_date',
                    'value'   => $now,
                    'compare' => '>='
                )
        ),
        'orderby' => 'meta_value',
        'order' => 'DESC',

1 个答案:

答案 0 :(得分:0)

您可以使用meta_query,例如:

$args = array(
    'meta_query' => array(
    'relation' => 'OR',
        array(
            'key'     => 'conference_start_date',
            'compare' => 'EXISTS'
        ),
        array(
            'key' => 'event_start_date',
            'compare' => 'EXISTS'
        ),
    ),
);

您可以在此处找到更多信息和示例:http://codex.wordpress.org/Class_Reference/WP_Query