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?
@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):
Hat-tip to this answer for the sorting: https://stackoverflow.com/a/10159521
Okay i’ve got it figured out!
Instead of calling
get_field
as thepost_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 aget_posts
. That way you have access to all the array options ofget_posts
before running the loop.Thanks for your help!
found my answer thanks to: http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1
I haven’t tested, But it should work for you with little adaption !