我有自定义post=\'events\',每个事件都有自定义字段“event start date”。我想显示我正在使用以下代码的即将到来的事件。
$date_today = time();
$meta_query_args=array(\'key\' => \'event-start-date\',
\'value\' => $date_today,
\'compare\' => \'>\',
);
$loop = new WP_Query( array(
\'post_type\' => \'events\',
\'posts_per_page\' => 5 ,
\'meta_key\' => \'event-start-date\',
\'orderby\' => \'event-start-date\',
\'order\'=>\'desc\',
\'meta_query\' => $meta_query_args
)
);
但这给了我所有的帖子,甚至那些还没有发布的帖子,比如
\'event-start-date\' < $date_today
.
我做错了什么?
最合适的回答,由SO网友:terminator 整理而成
只是稍微更改了一下查询
$meta_query_args=array(
\'relation\' => \'AND\',
array(
\'key\' => \'event-start-date\',
\'value\' => $date_today,
\'compare\' => \'<\',
)
);
$loop = new WP_Query( array(
\'post_type\' => \'events\',
\'posts_per_page\' => 5 ,
\'meta_key\' => \'event-start-date\',
\'orderby\' => \'event-start-date\',
\'order\'=>\'desc\',
\'meta_query\' => $meta_query_args
)
);
if($loop->have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
但谁能解释一下“关系”=>“和”在做什么