过滤掉比当前日期早的事件帖子 [英] Filter out event posts older than current date

查看:45
本文介绍了过滤掉比当前日期早的事件帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤日期早于当前日期的事件帖子?我的当前代码在事件已过期的情况下向该事件添加了一个过期的类,但是由于我需要显示接下来的5条即将发布的帖子,因此当前代码实际上根本不显示任何事件的帖子...这是我的代码....

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....

<?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;?>

请帮助某人:(

推荐答案

好,您可以使用meta_value比较日期,但请记住,这些日期必须采用Mysql格式YYYY-MM-DD,以便在因此,您的自定义字段必须采用这种格式,或者在使用之前需要在代码中重新格式化。这是Wordpress Stack上的一个很好的线程;

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. 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;

https://wordpress.stackexchange.com/questions/18303/未能比较元查询中的日期

我尚未对此进行测试,但它应该为您指明正确的方向;

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; ?>

这篇关于过滤掉比当前日期早的事件帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆