Support

Account

Home Forums Front-end Issues acf show custom field on date range

Helping

acf show custom field on date range

  • I have a custom field that i only want to show if today’s date is between a date range. I have a start & end date selected in ACF.

    <?php 
    $today = date('Ymd');
    $args = array (
    'meta_key'  => 'date',
    'meta_query' => array(
        array(
        'key'       => 'offer_start_date',
        'compare'   => '<=',
        'value'     => $today,
            ),
         array(
         'key'      => 'offer_end_date',
         'compare'  => '>=',
         'value'    => $today,
         )
        ),
    );
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();    ?>
    
    <div class="sing_whole_post" id="post-<?php the_ID(); ?>">
    <?php if( !! get_field('date') ) { ?><?php the_field('offer'); ?><?php } ?>
    </div>
    <?php endwhile; else: ?>

    thank you in advance.

    Rich

  • Hi Rich…

    Since you want both comparisons to be true, you can include an “AND relation” within the query:

    
    array(
      'relation' => 'AND',
      array(
        'key' => 'offer_start_date',
        'value' => $today,
        'compare' => '<='
      ),
      array(
        'key' => 'offer_end_date',
        'value' => $today,
        'compare' => '>='
      )
    )
    

    Also, here are more docs on date queries that may be useful.

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

The topic ‘acf show custom field on date range’ is closed to new replies.