[英]Filter out event posts older than current date
how do i filter out event posts who's date is older than the current date? 如何过滤日期早于当前日期的事件帖子? my current code adds an expired class to the event if that event has expired, but becuase i need to display the next 5 upcoming posts the current code actually displays no event posts at all... here's my code.... 我的当前代码在事件已过期的情况下向该事件添加了一个过期的类,但是由于我需要显示接下来的5条即将发布的帖子,因此当前代码实际上根本不显示任何事件的帖子...这是我的代码...。
<?php
wp_reset_query();
query_posts(array('post_type' => 'events',
'showposts' => 5,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
?>
<?php while (have_posts()) : the_post(); ?>
<?php
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentDate = new DateTime();
?>
<li class="<? if ($eventDate < $currentDate) { echo "expired"; } ?>">
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span></li>
<?php endwhile;?>
pleas help someone :( 请帮助某人:(
OK to answer your question. 可以回答您的问题。 You can compare dates using a meta_value, but bear in mind that those dates need to be in the Mysql format YYYY-MM-DD for a comparison to be made in the database. 您可以使用meta_value比较日期,但请记住,这些日期必须采用Mysql格式YYYY-MM-DD,以便在数据库中进行比较。 So your custom field needs to be in that format or you need to reformat it in your code before you use it. 因此,您的自定义字段必须采用这种格式,或者在使用之前需要在代码中对其重新格式化。 Here's a good thread on the Wordpress Stack on that; 这是Wordpress Stack上的一个很好的线程。
https://wordpress.stackexchange.com/questions/18303/fail-to-compare-dates-in-meta-query https://wordpress.stackexchange.com/questions/18303/fail-to-compare-dates-in-meta-query
I've not tested this but it should point you in the right direction; 我尚未对此进行测试,但是它应该为您指明正确的方向;
$eventDate = date('Y-m-t', get_field('event_date'));
$currentDate = date('Y-m-t');
$args = array(
'post_type' => 'events',
'posts_per_page' => '-1',
'orderby' => 'meta_value',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $eventDate,
'value' => $currentDate,
'compare' => '>',
'type' => 'DATE'
)
)
);
The other way to do it of course is just to pull all the posts into your query, set up a counter and display the first five which fulfil your criteria; 当然,另一种方法是将所有帖子拉入您的查询中,设置一个计数器并显示满足您条件的前五个;
<?php
$counter = 0;
query_posts(array('post_type' => 'events',
'posts_per_page' => -1,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
while (have_posts()) : the_post();
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentDate = new DateTime();
if ( ($eventDate > $currentDate) && $counter<5) : ?>
<li class="<? if ($eventDate < $currentDate) { echo "expired"; } ?>">
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span></li>
<?php
$counter++;
endif; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.