Support

Account

Home Forums General Issues Check date on ACF field

Helping

Check date on ACF field

  • Hi everyone!
    On a page I’m showing posts related from a date field that give me an output d/m/Y.

    Right now I’m using this code to show posts:

     $latest_post = get_field('latest_post');
    						 
           $args =   array(
           'post_type'=>'post',
           'cat'  =>  $latest_post,
           'order'  =>  'DATE',
           'posts_per_page'  =>  8,
           'meta_key' => 'art_date_from',
           'orderby' => 'art_date_from',
           'order' => 'ASC',						  
          );
    
           	$my_query = new WP_Query($args);
    	while ($my_query->have_posts()) : $my_query->the_post();
    						
    	$today = date("d/m/Y");
    	$date = get_field('art_date_from');

    Can I check if my ACF field date is newer or equal of “today” and show only post that date is like today or in the future? I try a use an if statement >= but seems not working or maybe I made something wrong. If I print the 2 date ($date and $today) I got the same date format. Maybe I need to change my ACF date field output?

    Many thanks for any advices!

    Regards

  • you cannot compare dates in “d/m/y” format. In order to compare dates they must be in some format that has year first, month second and day last.

    You do not need to change your date format. You can get the date field without formatting by supplying the 3rd argument in get_field()

    
    $date = get_field('art_date_from', false, false);
    

    This will return the date formatted using in “Ymd”

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

You must be logged in to reply to this topic.