Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

Any ideas?

like image 634
Michael Ray-Von Avatar asked Mar 24 '23 01:03

Michael Ray-Von


2 Answers

@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

like image 163
crdunst Avatar answered Apr 05 '23 20:04

crdunst


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

like image 41
Michael Ray-Von Avatar answered Apr 05 '23 21:04

Michael Ray-Von