在高级自定义字段中按日期排序对象

时间:2013-05-08 02:45:54

标签: php wordpress custom-fields

您好我在高级自定义字段中有一个帖子对象字段,我希望返回多个帖子,按日期排序。我有来自那些帖子的自定义字段数据返回正常,但邮件对象按邮政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; ?>

这将从调用此帖子的帖子的“帖子对象”字段中选择的每个帖子返回文本自定义字段“标题”和“日期”。

我希望帖子按照发布日期的顺序返回此处。

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

好的,我已经明白了!

不是将get_field称为post_objects,而是将其称为变量,只是为了获取相关帖子的ID,然后在数组中使用$args的{ get_posts。这样,在运行循环之前,您可以访问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; ?>

感谢您的帮助!

感谢我的回答:http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1

答案 1 :(得分:3)

@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

答案 2 :(得分:0)

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

我没有经过测试,但它应该适合你,很少适应!

相关问题