Filter out event posts older than current date

2019-08-13 02:58发布

问题:

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

pleas help someone :(

回答1:

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