Support

Account

Home Forums General Issues Ordering/Filtering wp_query using ACF Date field Reply To: Ordering/Filtering wp_query using ACF Date field

  • thanks john.

    so if i read you correctly i should end up with something that looks a bit like this?

    <h4>Recent Events</h4>
    <?php 
    //Set server timezone to GMT
    date_default_timezone_set('Europe/London'); 
    //Past cutoff date - the arg will look between today's date and this future date to see if the post fall within the 2 dates
    $date_1 = date('Ymd', strtotime("-24 months")); 
    //Today's date 
    $date_2 = date('Ymd', strtotime("now"));
    ?>		
    <?php
    $recent_args = array(
    'post_type'  => 'event',
    'meta_query' => array(
    'relation' => 'AND',
    array(
    'key'     => 'end_date',
    'value'   => $date_2,
    'compare' => '<',
    ),
    array(
    'key'		=> 'end_date',
    'compare' 	=> 'BETWEEN',
    'type' 		=> 'DATE',
    'value' 	=> array($date_1, $date_2),
    ),
    // if no end date has been set use start date
    array(
    'key'       => 'date',
    'compare' 	=> 'BETWEEN',
    'type' 		=> 'DATE',
    'value' 	=> array($date_1, $date_2),
    ),
    ),
    'orderby' 		=> 'meta_value_num',
    'order'        	=> 'DESC',
    'nopaging'     	=> true
    );
    ?>
    <?php 
    // the recent events query
    $recent_query = new WP_Query( $recent_args ); 
    ?>
    <?php if ( $recent_query->have_posts() ) : ?>
    <?php while ( $recent_query->have_posts() ) : $recent_query->the_post(); ?>

    the problem is that the first AND excludes any events in the past which do not have an end date at all (this is likely to be most of them). the above is working fine for events which have both start and end determined now but i need also to include events that only have one date, as in the first feed on the page.

    you can see what i mean here: http://wpa.matmartin.co.uk/events/ – there is a one-day event in the system which should be appearing in that recent list, too.