Php 按日期顺序发布高级自定义字段中的对象
嗨,我在高级自定义字段中有一个Post对象字段,我想返回多个Post,按日期排序。我有来自这些帖子的自定义字段数据,返回的很好,但是帖子对象按照帖子ID的顺序返回。我希望它们按照帖子发布的日期排序Php 按日期顺序发布高级自定义字段中的对象,php,wordpress,custom-fields,Php,Wordpress,Custom Fields,嗨,我在高级自定义字段中有一个Post对象字段,我想返回多个Post,按日期排序。我有来自这些帖子的自定义字段数据,返回的很好,但是帖子对象按照帖子ID的顺序返回。我希望它们按照帖子发布的日期排序 <?php $post_objects = get_field('exhibitions'); if( $post_objects ): ?> <?php foreach( $post_objects as $post_object): ?> <a href="
<?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; ?>
这将返回在调用它的帖子的帖子对象字段中选择的每篇帖子的文本自定义字段“title”和“dates”
我希望这些帖子按发布日期的顺序返回这里
有什么想法吗?
<?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();
}
结束时;
wp_reset_postdata();
}
我还没有测试,但它应该对你的小适应工作 好吧,我已经弄明白了 您不需要将
get_字段
作为post_对象
调用,而是将其作为变量调用,以获取相关帖子的ID,然后将其用于get_帖子
的$args
数组中。这样,您就可以在运行循环之前访问get_posts
的所有数组选项
<?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; ?>
谢谢你的帮助
找到我的答案是因为:@Michael Ray Von-你的答案是有效的,但它涉及到两次从数据库获取相同的数据。相反,您可以对初始ACF查询中返回的post数据进行排序,而不是运行额外的查询。(post_日期以字符串形式返回,因此您可以strcmp它):
好的,谢谢。看起来不错。唯一的问题是,它返回了所有可能的答案“The_field(‘exhibitions’);”而不仅仅是在“post_对象”字段中选择的对象
<?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();
?>