Support

Account

Home Forums ACF PRO WP_Query orderby ACF date picker field

Solving

WP_Query orderby ACF date picker field

  • Hello,

    I’m trying to order my custom post type ‘training’ by my ACF date field ‘training__date’. This is how my code looks like:

    $today = date('Ymd');
    
    // Arguments for the query
    $args = array(
      'post_type' => 'training',
      'posts_per_page' => -1,
      'post_status' => 'publish',
      'meta_query' => array(
        array(
          'key' => 'training__date',
          'value' => $today,
          'type' => 'DATE',
          'compare' => '>='
        )
      ),
      'meta_key' => 'training__date',
      'order_by' => 'meta_value_num',
      'order' => 'ASC',
    );
    
    $training_query = new WP_Query($args);

    The strange thing is that the meta_query is working, but the ordering by date isn’t. Am I doing something wrong? I also tried changing the order_by value to meta_value instead of meta_value_num, but that didn’t make a difference.

    The return format of the date field is Ymd.
    WP version: 4.8
    ACF PRO 5.5.14

    Thanks in advance.

  • change order_by to orderby

  • I have the same issue – also trying to order by a custom date field. So the code looks almost identical. I have orderby written correctly but still have this issue.
    I’ve also tried ‘orderby’ => ‘meta_value’ and ‘orderby’ => ‘meta_value_num’ with no difference.
    My args are:

    $args = array(
    ‘post_type’ => array( $post_type ),
    ‘post_status’ => array( ‘publish’ ),
    ‘posts_per_page’ => -1,
    ‘tax_query’ => array(
    array (
    ‘taxonomy’ => $taxonomy,
    ‘terms’ => $term_id,
    )
    ),
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘date’,
    ‘compare’ => ‘>=’,
    ‘value’ => $date_now,
    ‘type’ => ‘DATETIME’
    ),
    ),
    ‘order’ => ‘DESC’,
    ‘orderby’ => ‘meta_value’,
    ‘meta_key’ => ‘date’,
    ‘meta_type’ => ‘DATETIME’,

    );

    I’d be grateful if you have any pointers 🙂

  • Okay – I tried this again. ‘orderby’ => ‘meta_value_num’ solved the issue. As long as the date format for this field is set to YYYYMMDD

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

The topic ‘WP_Query orderby ACF date picker field’ is closed to new replies.