Support

Account

Home Forums ACF PRO WP_Query based on ACF values (get values before loop)

Helping

WP_Query based on ACF values (get values before loop)

  • Hello

    I have a custom post type of “events” where an event has a required field of “start date” and an optional field of “end date”.

    I’m creating a query to display events only in the future, based on either the existence of a “start date” (if only has a start date) or an “end date” (if has start date and end date).

    The problem is when I create a conditional to either use “start date” or “end date”, I cannot get these values, presumably because I’m outside the loop.

    Is there any way to get an ACF field value and use this within the WP Query before the query actually runs?

    The query:

    $events_start_date = get_field('events_date_start');
    $events_end_date = get_field('events_date_end');
    
    if ($events_start_date && $events_end_date) {
      $key = 'events_date_end';
    }
    else {
      $key = 'events_date_start';
    }
    
    $date = current_time('Ymd');
    
    $args = array(
    'post_type' => 'events',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'meta_query' => array(
    array(
    'key' => $key,
    'compare' => '>=',
    'value' => $date,
    )
    ),
    'meta_key' => 'events_date_start',
    'orderby' => 'meta_value',
    'order' => 'ASC'
    );
    
    $the_query = new WP_Query( $args );
    
    if( $the_query->have_posts() ): ?>
    
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
    // Content
    
    endwhile;
    
    else :
    
    // Nothing
    
    endif; wp_reset_postdata();
  • Hi @juxprose

    maybe you can use this query instead?

    $args = array(
        'post_type' => 'events',
        'post_status' => 'publish',
        'posts_per_page' => '-1',
        'meta_query' => array(
            'relation' => 'OR', 
            array(
                'relation' => 'AND', // if no end date
                array(
                    'key' => events_date_end,
                    'compare' => '=',
                    'value' => '',
                ),
                array(
                    'key' => events_date_start,
                    'compare' => '>=',
                    'value' => $date,
                ),
            ),
            array(
                'relation' => 'AND', //if have end date
                array(
                    'key' => events_date_end,
                    'compare' => '!=',
                    'value' => '',
                ),
                array(
                    'key' => events_date_end,
                    'compare' => '>=',
                    'value' => $date,
                ),
            ),
        ),
        'meta_key' => 'events_date_start',
        'orderby' => 'meta_value',
        'order' => 'ASC'
    );

    Keep in mind that I haven’t tested it yet. Please fix it if you find some errors.

    I hope this helps.

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

You must be logged in to reply to this topic.