Post Objects in Advanced Custom Fields order by date

Hi I have a Post Object field in Advanced Custom Fields that I want to return multiple posts, ordered by date. I have the custom field data from those posts returning fine, but the Post Objects return in order of the Post ID. I want them to be ordered by the date that the post was published.

<?php $post_objects = get_field('exhibitions');

if( $post_objects ): ?>

 <?php foreach( $post_objects as $post_object): ?>

 <a href="<?php echo get_permalink($post_object->ID); ?>">
 <div style="display: inline-block">

  <? if( get_field( 'title', $post_object->ID) ): ?>
   <em><?php the_field('title', $post_object->ID); ?></em><br>
  <?php endif; ?>

  <? if( get_field( 'dates', $post_object->ID) ): ?>
   <?php the_field('dates', $post_object->ID); ?>
  <?php endif; ?>

 </div>
 </a>
 <br><br>

 <?php endforeach; ?>

<?php endif; ?>

This returns the text custom fields ‘title’ and ‘dates’ from each post thats selected in the Post Objects field on the post where this is called.

Read More

I want the posts to return here by order of their publish date.

Any ideas?

Related posts

Leave a Reply

3 comments

  1. @Michael Ray-Von – your answer worked, but it involved getting the same data from the db twice. Instead you can just sort the post data returned in your initial ACF query rather than running the extra query. (The post_date is returned as a string so you can strcmp it):

    <?php
    // get the posts from ACF
    $custom_posts = get_field('your_posts_field');
    
    // sort the posts by post date, but you can also sort on ID or whatever
    usort($custom_posts, function($a, $b) {
        return strcmp($b->post_date,$a->post_date);
    });
    
    // write them out
    foreach ($custom_posts as $post) :  setup_postdata($post); ?>
    
            <article>
                    <h1><?php the_title();?></h1>
                    <?php the_excerpt(); ?>     
            </article>
    
    <?php
    endforeach;
    wp_reset_query();
    ?>
    

    Hat-tip to this answer for the sorting: https://stackoverflow.com/a/10159521

  2. Okay i’ve got it figured out!

    Instead of calling get_field as the post_objects, you call it as a variable just to get the IDs of relevant posts, and then use that in an array for the $args of a get_posts. That way you have access to all the array options of get_posts before running the loop.

    <?php 
    
    $ids = get_field('exhibitions', false, false);
    
    $args = array(
      'post__in' => $ids,
      'orderby' => 'post_date',
    );
    
    $post_objects = get_posts( $args );
    
    if( $post_objects ): ?>
    
    <?php foreach( $post_objects as $post_object): ?>
    
      <a href="<?php echo get_permalink($post_object->ID); ?>">
      <div style="display: inline-block">
    
      <? if( get_field( 'title', $post_object->ID) ): ?>
       <em><?php the_field('title', $post_object->ID); ?></em><br>
      <?php endif; ?>
    
      <? if( get_field( 'dates', $post_object->ID) ): ?>
       <?php the_field('dates', $post_object->ID); ?>
      <?php endif; ?>
    
      </div>
      </a>
      <br><br>
    
    <?php endforeach; ?>
    
    <?php endif; ?>
    

    Thanks for your help!

    found my answer thanks to: http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1

  3. <?php $post_objects = get_field('exhibitions');
    $args = array (
        'orderby'=>'date',
    );
    $the_query = new WP_Query( $args );
    if($the_query->have_posts()) { 
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
            <a href="<?php echo get_permalink($post_object->ID); ?>">
     <div style="display: inline-block">
    
    <? if( get_field( 'title', $post_object->ID) ): ?>
    <em><?php the_field('title', $post_object->ID); ?></em><br>
    <?php endif; ?>
    
    <? if( get_field( 'dates', $post_object->ID) ): ?>
    <?php the_field('dates', $post_object->ID); ?>
    <?php endif; ?>`  </div>
    </a>
    <br><br>
    endwhile;
        wp_reset_postdata();
    }
    

    I haven’t tested, But it should work for you with little adaption !