Support

Account

Home Forums General Issues Get posts with date on or after current date

Solved

Get posts with date on or after current date

  • I have a post category called events where I some custom fields. One is call event_date and it uses the Date.time piker ACF plugin. I want to retrieve post based on that even_date field when the event_date is on or after the current date.

    I have this:

    <?php
        $args = array(
           'meta_key' => 'event_date',
           'orderby' => 'meta_value',
           'order' => 'ASC',
           'cat' => '2'
        );
    
        $the_query = new WP_Query( $args );
        
        
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
                
                $the_query->the_post(); ?>
    
                <div class="home-post-area">
    
                    <div class="home-post-image">
                        <?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
                      <div class="home-post-image-overlay">
                        <h3><a href="<?php the_permalink() ?>"><?php the_field('event_date'); ?> <?php the_field('event_price'); ?></a></h3>
                        <h4><a href="<?php the_permalink() ?>"><?php the_title();?></a></h4>
                      </div>
                    </div>
    
                    <div class="home-post-content">
                        <?php the_excerpt();?>
    
                    <p><a href="<?php the_permalink() ?>"><strong>>> MORE</strong></a></p>
    
                    </div>
    
                </div>
    
            <?php } } else { ?>
    
            <h2>Not Found</h2>
    
        <?php //endif; ?>
        <?php } ?>
        <?php wp_reset_postdata(); ?>

    and it gets the vents and sorts them correctly by the event_date field but I need to know how I add the additional condition of the event_date being equal to or greater than the current date. Any assistant would be appreciated. Thanks!

  • Hi,

    you need to specify your first meta_query in more detail.

    
    $today = date('Ymd');
    $args = array (
        'post_type' => 'events',
        'order' => 'ASC',
        'cat' => '2',
        'meta_query' => array(
    	     array(
    	        'key'		=> 'event_date',
    	        'compare'	=> '>=',
    	        'value'		=> $today,
    	    )
        ),
    );
    

    This is an approximation of the code based on what I can gather from your example. The important part is just to add the ‘meta_query’ in your standard query and you’ll be set.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Get posts with date on or after current date’ is closed to new replies.