我试图显示自定义日期字段值早于今天的自定义帖子类型。它可以在今天之前显示帖子,但顺序仅基于发布日期。我想将结果排序为meta_key
.
我尝试了以下代码,但我不明白为什么它不起作用:
query_posts( array(
\'post_type\' => \'soiree\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'date_de_concert\',
\'meta_query\' => array(
array(
\'key\' => \'date_de_concert\',
\'posts_per_page\' => \'-1\',
\'value\' => date( \'Y-m-d\' ),
\'compare\' => \'<\',
\'type\' => \'DATE\',
\'orderby\' => \'value\',
\'order\' => \'DESC\',
)
)
) );
最合适的回答,由SO网友:benny-ben 整理而成
我更喜欢使用WP\\U查询(有关更多信息,请阅读“When should you use WP_Query vs query_posts() vs get_posts()?“”,请尝试以下方法:
$args = array(
\'post_type\' => \'soiree\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'orderby\' => \'date_de_concert\',
// you don\'t need \'meta_key\' => \'date_de_concert\' when using meta_query
//\'meta_key\' => \'date_de_concert\',
\'order\' => \'DESC\',
\'meta_query\' => array(
array(
\'key\' => \'date_de_concert\',
\'value\' => strtotime( \'today\' ),
\'compare\' => \'<\'
)
)
);
$concert_query = new WP_Query( $args );
if ( $concert_query->have_posts() ) : while ( $concert_query->have_posts() ) : $concert_query->the_post();