我试图查询自定义帖子类型,按自定义元值排序,并仅显示具有不同自定义元值的帖子。
我能够独立完成每一项工作,但不能同时完成这两项工作。
这是当前代码。
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'shows\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'start_date\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'meta_key\' => \'show_location\',
\'meta_value\' => \'second\'
)
)
);
// get results
$the_query = new WP_Query( $args );
我的
NEW 根据以下答案进行编码:
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'shows\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'start_date\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'show_location\',
\'value\' => \'second\'
)
)
);
最合适的回答,由SO网友:Karun 整理而成
对于多个元值,必须使用“IN”
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'shows\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'start_date\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'show_location\',
\'value\' => array(\'second\',\'third\'),
\'compare\' => \'IN\'
)
)
);
// get results
$the_query = new WP_Query( $args );
IN-comparison检查post是否与“meta\\u value”数组中存在的值一起可用。