我试图将“Featured1314”类别中的帖子分为两个列表,即将发生的事件>=今天的日期,过去发生的事件为<;今天的日期。我在帖子中添加了自定义字段,名称为“concert\\u date”,值与日期格式Ymd匹配(例如:20140611)
我在下面使用的代码很接近,第一个查询的结果正确,但第二个查询的结果与第一个查询的结果重复。
<h1>2013-14 Concert Season</h1>
<?php
$today = date(\'Ymd\');
$args1 = array(
\'type\' => \'post\',
\'category_slug\' => \'Featured1314\',
\'meta_key\' => \'concert_date\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'concert_date\',
\'value\' => $today,
\'compare\' => \'>=\'
)
),
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
);
$upcoming_query = new WP_Query($args1);
if( $upcoming_query->have_posts() ) { ?>
<h2>Upcoming Concerts</h2>
<ul><?php while ( $upcoming_query->have_posts() ) { $upcoming_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1><p><?php the_excerpt() ?></p></li>
<?php } }?>
</ul>
<?php
rewind_posts();
$today = date(\'Ymd\');
$args2 = array(
\'type\' => \'post\',
\'category_slug\' => \'Featured1314\',
\'meta_key\' => \'concert_date\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'concert_date\',
\'value\' => $today,
\'compare\' => \'<\'
)
),
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
);
$past_query = new WP_Query($args2);
if( $past_query->have_posts() ) {?>
<h2>Past Concerts</h2>
<ul><?php while ( $past_query->have_posts() ) { $past_query->next_post(); ?>
<li id="post-<?php the_ID(); ?>"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1><p><?php the_excerpt() ?></p></li>
<?php } }?>
</ul>
最合适的回答,由SO网友:Howdy_McGee 整理而成
您可以尝试这样做,这主要是代码清理,但可能会解决您的问题。我删除了relation
从…起meta_query
因为只有当您的查询中有多个元键时才需要它,而您没有rewind_posts()
具有wp_reset_postdata()
最后,你结束了IF
比您的UL
标记,如果为false,则将生成孤儿</ul>
\'他到处游荡,这从来都不是好事。
<h1>2013-14 Concert Season</h1>
<?php
$today = date(\'Ymd\');
$args1 = array(
\'type\' => \'post\',
\'category_slug\' => \'Featured1314\',
\'meta_key\' => \'concert_date\',
\'meta_query\' => array(
array(
\'key\' => \'concert_date\',
\'value\' => $today,
\'compare\' => \'>=\'
)
),
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
);
$upcoming_query = new WP_Query($args1);
if( $upcoming_query->have_posts() ) { ?>
<h2>Upcoming Concerts</h2>
<ul><?php while ( $upcoming_query->have_posts() ) { $upcoming_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1><p><?php the_excerpt() ?></p></li>
<?php } // endwhile ?>
</ul>
<?php } // endif;
wp_reset_postdata();
$args2 = array(
\'type\' => \'post\',
\'category_slug\' => \'Featured1314\',
\'meta_key\' => \'concert_date\',
\'meta_query\' => array(
array(
\'key\' => \'concert_date\',
\'value\' => $today,
\'compare\' => \'<\'
)
),
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
);
$past_query = new WP_Query($args2);
if( $past_query->have_posts() ) {?>
<h2>Past Concerts</h2>
<ul><?php while ( $past_query->have_posts() ) { $past_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1><p><?php the_excerpt() ?></p></li>
<?php } //endwhile; ?>
</ul>
<?php } // endif;