您好,我在高级自定义字段中有一个帖子对象字段,我想返回多个帖子,按日期排序。我有来自那些返回正常的帖子的自定义字段数据,但帖子对象按帖子 ID 的顺序返回。我希望它们在帖子发布之日之前订购。
<?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; ?>
这将返回在调用它的帖子的“帖子对象”字段中选择的每个帖子的文本自定义字段“标题”和“日期”。
我希望帖子按发布日期的顺序返回此处。
有什么想法吗?
最佳答案
@Michael Ray-Von - 你的答案有效,但它涉及两次从数据库中获取相同的数据。相反,您可以只对初始 ACF 查询中返回的发布数据进行排序,而不是运行额外的查询。 (post_date 以字符串形式返回,因此您可以对其进行 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();
?>
对这个排序答案的提示:https://stackoverflow.com/a/10159521
关于php - 按日期顺序在高级自定义字段中发布对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16431853/