Email objects in advanced custom fields by date

Hi I have a Post Object field in Advanced Custom Fields that I want to return a few posts sorted by date. I have user field data from these messages returning a fine, but Post Objects are returned in the order of the Post ID. I want them to be ordered by the publication date of publication.

<?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 custom text fields "title" and "date" from each column selected in the Post Objects field in the column where it is called.

I want messages to be returned here in order of publication date.

Any ideas?

+4
source share
3 answers

OK I understood!

Instead of calling get_field as post_objects you call it as a variable to get the identifiers of the corresponding messages, and then use them in the array for $args for get_posts . Thus, before starting the loop, you will get access to all parameters of the get_posts array.

 <?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; ?> 

Thank you for your help!

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

+3
source

@Michael Ray-Von - your answer worked, but it included getting the same data from db twice. Instead, you can simply sort the message data returned in the original ACF request, rather than performing an additional request. (Post_date is returned as a string, so you can strcmp):

 <?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(); ?> 

Tip for answering this question for sorting: fooobar.com/questions/18951 / ...

+2
source
 <?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 have not tested, but it should work for you with a little adaptation!

0
source

Source: https://habr.com/ru/post/1479661/


All Articles