1

I use the bellow template to display the first event from an ascendent list of custom post type events, created by a third party plugin. What I want is to exclude old events, displaying only the one actual, if we have one today, or the nearest. The event start date is saved in a meta_key with a value in the "Y-m-d" format (ex: 2017-10-27). How to accomplish this?

<?php

    wp_reset_query();

    $args = array(
        'post_type'         => 'events',
        'posts_per_page'    => 1,
        'meta_key'          => 'event_start_date',
        'order'             => 'ASC'
    );

    $featured = new WP_Query($args);

    if ($featured->have_posts()): 
        while($featured->have_posts()): $featured->the_post();

            $featured_event_start = $post->event_start_date;
            $featured_event_end = $post->event_end_date;

            if (has_post_thumbnail()) : ?>
                <div class="featured-event-outer">
                    <div class="featured-event-inner">
                        <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium_large'); ?></a></figure>
                        <h3 class="featured-event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <h3 class="featured-event-date">
                            <a href="<?php the_permalink(); ?>"><?php echo $featured_event_start;
                                if ( $featured_event_end && $featured_event_end != $featured_event_start ) { echo ' - ' . $featured_event_end; } ?>
                            </a>
                        </h3>
                    </div>
                </div>
            <?php endif; ?>

        <?php endwhile;
            else: ?>
                <!-- Placeholder content goes here -->
    <?php endif;

    wp_reset_postdata();
?>
2

You need something like the following:

$args = array(
    'post_type'         => 'events',
    'posts_per_page'    => 1,
    'meta_query'        => array(
        'meta_key'          => 'event_start_date',
        'type'              => 'DATETIME',  // You can also try changing it to TIME or DATE if it doesn't work
        'meta_value'        => date( "Y-m-d" ),
        'meta_compare'      => '>',
    ),
    'order'             => 'ASC'
);
  • I already tried your solution, but it doesn't work for me, I don't understand why. – Iurie Malai Aug 30 '17 at 8:06
2

Update: the code worked for me after replacing event_start_date with _event_start_date in 'meta_key'

$args = array(
    'post_type' => 'events', // Tell WordPress which post type we want
    'orderby' => 'meta_value', // We want to organize the events by date    
    'meta_key' => '_event_start_date', // Grab the "start date" field created via "More Fields" plugin (stored in YYYY-MM-DD format)
    'order' => 'ASC', // ASC is the other option    
    'posts_per_page' => '1', // Let's show only one / the first event.
    'meta_query' => array( // WordPress has all the results, now, return only the events after today's date
        array(
            'key' => '_event_start_date', // Check the start date field
            'value' => date("Y-m-d"), // Set today's date (note the similar format)
            'compare' => '>=', // Return the ones greater than or equal to today's date
            'type' => 'DATE' // Let WordPress know we're working with date
        )
    )
);
0

OK, with some help from @jonathan-wold, I figured out how to solve my problem. In my initial code I just replaced the $args part, accordingly to mentioned author, and this worked like a charm. This is the code:

$args = array(
    'post_type' => 'events', // Tell WordPress which post type we want
    'orderby' => 'meta_value', // We want to organize the events by date    
    'meta_key' => 'event_start_date', // Grab the "start date" field created via "More Fields" plugin (stored in YYYY-MM-DD format)
    'order' => 'ASC', // ASC is the other option    
    'posts_per_page' => '1', // Let's show only one / the first event.
    'meta_query' => array( // WordPress has all the results, now, return only the events after today's date
        array(
            'key' => 'event_start_date', // Check the start date field
            'value' => date("Y-m-d"), // Set today's date (note the similar format)
            'compare' => '>=', // Return the ones greater than or equal to today's date
            'type' => 'DATE' // Let WordPress know we're working with date
        )
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.