Hi I am trying to sort a relationship in ACF by date, this is the code I have so far but $the_query is returning empty.

- $journalEntries = get_field('author', false, false)
- $args = array('post__in' => $journalEntries,
    'orderby' => 'date',
    'order' => 'DESC',
  )
- $the_query = new WP_Query($args)

- if ( $the_query->have_posts() )
  - $dataLink == 1 ? $active="active" : $active=""
  .tab-link.tab-container.journal(data-tab-link="#{$dataLink}" class="#{$active}")
    .tab-content                
      %ul
        - while ( $the_query->have_posts() )
          - $the_query->the_post()
          %li.stagger
            %a(href="#{get_the_permalink()}")
              - echo get_the_title()
  - $dataLink++
  - wp_reset_query()

This is what var_dump($the_query) returns:

object(WP_Query)[2490]
  public 'query' => 
    array (size=3)
      'post__in' => 
        array (size=9)
          0 => int 2687
          1 => int 2691
          2 => int 2693
          3 => int 2695
          4 => int 2697
          5 => int 2700
          6 => int 2703
          7 => int 2789
          8 => int 2791
      'orderby' => string 'date' (length=4)
      'order' => string 'DESC' (length=4)
  public 'query_vars' => 
    array (size=65)
      'post__in' => 
        array (size=9)
          0 => int 2687
          1 => int 2691
          2 => int 2693
          3 => int 2695
          4 => int 2697
          5 => int 2700
          6 => int 2703
          7 => int 2789
          8 => int 2791
      'orderby' => string 'date' (length=4)
      'order' => string 'DESC' (length=4)
      'error' => string '' (length=0)
      'm' => string '' (length=0)
      'p' => int 0
      'post_parent' => string '' (length=0)
      'subpost' => string '' (length=0)
      'subpost_id' => string '' (length=0)
      'attachment' => string '' (length=0)
      'attachment_id' => int 0
      'name' => string '' (length=0)
      'static' => string '' (length=0)
      'pagename' => string '' (length=0)
      'page_id' => int 0
      'second' => string '' (length=0)
      'minute' => string '' (length=0)
      'hour' => string '' (length=0)
      'day' => int 0
      'monthnum' => int 0
      'year' => int 0
      'w' => int 0
      'category_name' => string '' (length=0)
      'tag' => string '' (length=0)
      'cat' => string '' (length=0)
      'tag_id' => string '' (length=0)
      'author' => string '' (length=0)
      'author_name' => string '' (length=0)
      'feed' => string '' (length=0)
      'tb' => string '' (length=0)
      'paged' => int 0
      'meta_key' => string '' (length=0)
      'meta_value' => string '' (length=0)
      'preview' => string '' (length=0)
      's' => string '' (length=0)
      'sentence' => string '' (length=0)
      'title' => string '' (length=0)
      'fields' => string '' (length=0)
      'menu_order' => string '' (length=0)
      'embed' => string '' (length=0)
      'category__in' => 
        array (size=0)
          empty
      'category__not_in' => 
        array (size=0)
          empty
      'category__and' => 
        array (size=0)
          empty
      'post__not_in' => 
        array (size=0)
          empty
      'post_name__in' => 
        array (size=0)
          empty
      'tag__in' => 
        array (size=0)
          empty
      'tag__not_in' => 
        array (size=0)
          empty
      'tag__and' => 
        array (size=0)
          empty
      'tag_slug__in' => 
        array (size=0)
          empty
      'tag_slug__and' => 
        array (size=0)
          empty
      'post_parent__in' => 
        array (size=0)
          empty
      'post_parent__not_in' => 
        array (size=0)
          empty
      'author__in' => 
        array (size=0)
          empty
      'author__not_in' => 
        array (size=0)
          empty
      'ignore_sticky_posts' => boolean false
      'suppress_filters' => boolean false
      'cache_results' => boolean true
      'update_post_term_cache' => boolean true
      'lazy_load_term_meta' => boolean true
      'update_post_meta_cache' => boolean true
      'post_type' => string '' (length=0)
      'posts_per_page' => int 10
      'nopaging' => boolean false
      'comments_per_page' => string '50' (length=2)
      'no_found_rows' => boolean false
  public 'tax_query' => 
    object(WP_Tax_Query)[2514]
      public 'queries' => 
        array (size=0)
          empty
      public 'relation' => string 'AND' (length=3)
      protected 'table_aliases' => 
        array (size=0)
          empty
      public 'queried_terms' => 
        array (size=0)
          empty
      public 'primary_table' => string 'wp_posts' (length=8)
      public 'primary_id_column' => string 'ID' (length=2)
  public 'meta_query' => 
    object(WP_Meta_Query)[2513]
      public 'queries' => 
        array (size=0)
          empty
      public 'relation' => null
      public 'meta_table' => null
      public 'meta_id_column' => null
      public 'primary_table' => null
      public 'primary_id_column' => null
      protected 'table_aliases' => 
        array (size=0)
          empty
      protected 'clauses' => 
        array (size=0)
          empty
      protected 'has_or_relation' => boolean false
  public 'date_query' => boolean false
  public 'request' => string 'SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.ID IN (2687,2691,2693,2695,2697,2700,2703,2789,2791) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC LIMIT 0, 10' (length=325)
  public 'posts' => 
    array (size=0)
      empty
  public 'post_count' => int 0
  public 'current_post' => int -1
  public 'in_the_loop' => boolean false
  public 'comment_count' => int 0
  public 'current_comment' => int -1
  public 'found_posts' => int 0
  public 'max_num_pages' => int 0
  public 'max_num_comment_pages' => int 0
  public 'is_single' => boolean false
  public 'is_preview' => boolean false
  public 'is_page' => boolean false
  public 'is_archive' => boolean false
  public 'is_date' => boolean false
  public 'is_year' => boolean false
  public 'is_month' => boolean false
  public 'is_day' => boolean false
  public 'is_time' => boolean false
  public 'is_author' => boolean false
  public 'is_category' => boolean false
  public 'is_tag' => boolean false
  public 'is_tax' => boolean false
  public 'is_search' => boolean false
  public 'is_feed' => boolean false
  public 'is_comment_feed' => boolean false
  public 'is_trackback' => boolean false
  public 'is_home' => boolean true
  public 'is_404' => boolean false
  public 'is_embed' => boolean false
  public 'is_paged' => boolean false
  public 'is_admin' => boolean false
  public 'is_attachment' => boolean false
  public 'is_singular' => boolean false
  public 'is_robots' => boolean false
  public 'is_posts_page' => boolean false
  public 'is_post_type_archive' => boolean false
  private 'query_vars_hash' => string '2ea1348ad53c0c8955b5eb02c6700ffb' (length=32)
  private 'query_vars_changed' => boolean false
  public 'thumbnails_cached' => boolean false
  private 'stopwords' => null
  private 'compat_fields' => 
    array (size=2)
      0 => string 'query_vars_hash' (length=15)
      1 => string 'query_vars_changed' (length=18)
  private 'compat_methods' => 
    array (size=2)
      0 => string 'init_query_flags' (length=16)
      1 => string 'parse_tax_query' (length=15)

I also tried editing functions.php with this but it didn't seem to work:

function my_post_object_query( $args, $field, $post )
{
    // modify the order
    $args['orderby'] = 'date';
    $args['order'] = 'DESC';

    return $args;
}

// filter for a specific field based on it's name
add_filter('acf/fields/post_object/query/name=author', 'my_post_object_query', 10, 3);
  • The $journalEntries IDs are a post_type = post? Because if you don't specify the post_type in the WP_Query it will default to 'post'. – gdaniel Aug 16 '17 at 16:54
  • OMG you are right! I thought the post type was post or news (tried both) but it was actually "journal", thanks! – Nuuu Aug 16 '17 at 17:15
  • Yep. No problem, have made this same mistake a few times myself. I will post my comment as the answer. – gdaniel Aug 16 '17 at 17:28
up vote 0 down vote accepted

The $journalEntries IDs are a post_type = post? Because if you don't specify the post_type in the WP_Query it will default to 'post'.

So your $args array should be:

$args = array(
    'post_type' => 'put_post_type_here',
    'post__in' => $journalEntries,
    'orderby' => 'date',
    'order' => 'DESC',
);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.