filter out post older than custom date field

i’m trying to hide posts whereby the custom date field is older than today’s date. the way my code is currently set up is to add a class called expired to the LI tag if older but its not playing ball…

            <?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 = date("Ymd");

                                    ?>

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

please help me someone:(

Related posts

Leave a Reply

1 comment

  1. At the moment you’re comparing a DateTime object against a string – you need to make sure you’re comparing equivalent data types:

    // Convert stored date to DateTime object
    $eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
    
    // Get the current date as a DateTime object
    $nowDate = new DateTime();
    
    // And compare them
    if ($eventDate == $nowDate) {
       // They're the same, woohoo
    } elseif ($eventDate < $nowDate) {
       // Expired
    }