Support

Account

Home Forums General Issues Custom query filtering & ordering by ACF fields

Solving

Custom query filtering & ordering by ACF fields

  • I am trying to create a custom query in my template file that filters posts by whether the end_date ACF is after today (event has not ended yet), and then order the output by the value in the event_start ACF. But I can’t seem to get my query right:

    $today = date('Ymd');
    $thePosts = new WP_Query(array(
    'post_type'        => 'post',
    'posts_per_page'   => 10,
    'meta_key' => 'event_start',
    'meta_query'       => array(
    	array(
    		'key'      => 'event_end',
    		'value'    => '".$today."',
    		'compare'  => '>'
    	)
    ),
    'order' => 'DESC',
    'orderby' => 'meta_value',
    'post_status' => 'publish',
    ));
  • 
    'meta_query'       => array(
    	array(
    		'key'      => 'event_end',
    		'value'    => $today,  // change this
    		'compare'  => '>'
    	)
    ),
    
  • John,
    I made this change, but am still not getting any results. I have one Event item that is set for the end of this year, so it should show up.

  • Are you using a date field or a date/time field?

    Is there a value in both fields?

  • The two fields are Date Picker fields. Both field have a value.

  • Honestly, I don’t know why it’s not working, unless it has something to do with the mix of meta settings. Try this

    
    $today = date('Ymd');
    $thePosts = new WP_Query(array(
    'post_type'        => 'post',
    'posts_per_page'   => 10,
    'meta_query'       => array(
    	array(
    		'key'      => 'event_end',
    		'value'    => '".$today."',
    		'compare'  => '>'
    	),
        'date_clause' => array(
          'key' => 'event_start'
          'compare' => 'EXISTS'
        )
    ),
    'orderby' => array('date_clause' => 'DESC'),
    'post_status' => 'publish',
    ));
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.