从几个配置文件中按日期排序新闻


Sort news by date from several profiles

我有几个配置文件,允许用户发布我想在一个长提要中显示的新闻,按日期顺序排序。

此时,此循环遍历每个配置文件,列出该配置文件中的新闻,然后移动到下一个配置文件,列出他们的新闻等等。它不会把所有的东西都混在一起。

我如何将每个配置文件中的所有新闻混合在一起并按日期排序?

<?php global $post;
$args = array('numberposts' => -1);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
   <?php while(the_repeater_field('news')): ?>
      <div class="social-news-item">
         <p class="social-news"><?php the_sub_field('news_description'); ?></p>
     <p class="social-company"><?php echo the_title(); ?></p>
      </div>
   <?php endwhile; ?>
<? endforeach; wp_reset_postdata(); ?>

日期字段为:

<?php the_sub_field('date'); ?>

如果您想以编程方式更改顺序,请查看PHP中的各种数组排序函数,特别是

  • uasort() -使用用户定义的比较函数对数组进行排序并维护索引关联
  • uksort() -使用自定义比较函数
  • 按键对数组进行排序
  • usort() -使用用户定义的比较函数
  • 按值对数组进行排序

但是对于wordpress checkout这个http://codex.wordpress.org/Template_Tags/get_posts

示例:

默认值:

<?php 
   $args = array(
    'posts_per_page'  => 5,
    'numberposts'     => 5,
    'offset'          => 0,
    'category'        => '',
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'include'         => '',
    'exclude'         => '',
    'meta_key'        => '',
    'meta_value'      => '',
    'post_type'       => 'post',
    'post_mime_type'  => '',
    'post_parent'     => '',
    'post_status'     => 'publish',
    'suppress_filters' => true );
 ?>

使用:

 <?php $posts_array = get_posts( $args ); ?> 

正如我在评论中所写的,我不知道wordpress,也许有些地方是错误的,但这个想法可能是好的。

如果可能的话,你可以先把所有的帖子放在一起,然后再排序。

要对它们进行分组,请使用一个临时数组来获取对象。所以不要输出你的div,而是在数组中"缓冲"它们。

例如,如果数组名是$posts,你可以在foreach循环中这样做:(我希望在the_sub_field('news_description');中您错过了echo,在这种情况下它将不起作用)

$var = &$posts[]; // this creates a new item in array, and $var is this element
$var->description = the_sub_field('news_description'); // *****
$var->title = the_title();
$var->date = ... // it is important, I don't know if you have it

因此,在最后,您的数组将包含来自所有用户(或配置文件)的所有帖子。现在用ussort函数来排序。您将需要编写自己的排序函数,在手册中它是"cmp",所以我使用相同的名称:

function cmp($a, $b){
  if($a->date > $b->date)
    return 1;
  elseif($a->date < $b->date)
    return -1;
  else
    return 0;
}

命名为

usort($posts, 'cmp');

然后需要在另一个foreach循环中输出数组。

但是,就像我说的,我不知道wordpress是否允许。

我想你说的" Profiles "是指" Authors "。

一旦你有了$myposts

中的所有帖子
$myposts = get_posts( $args );

遍历它们
foreach( $myposts as $key => $post )

(注意我已经添加了$key)

现在你可以用日期作为键创建你自己的数组

$my_array[$myposts[$key]->post_date][] = ...whatevever you want in the end

可以随意添加最后要显示的任何数据。让我们以文章的作者和标题为例:

$the_item[$myposts[$key]->post_author] = $myposts[$key]->post_title;
$my_array[$myposts[$key]->post_date][] = $the_item;

这将产生如下所示的数组:

[2007-11-19 22:46:37] => Array
    (
        [0] => Array
            (
                [3] => Title
                [2] => Another title
            )
    )
[2007-11-11 11:11:11] => Array
    (
        [0] => Array
            (
                [3] => Yet another title
                [2] => Foo
            )
        [1] => Array
            (
                [3] => Bar
                [2] => Yuck
            )

所有文章现在按日期排序,不同作者的文章"混合"。可以随意使用任何排序函数(如上所述)。您可能希望通过使用shuffle()来添加一点随机性…为了显示帖子,您将采取相反的方式:遍历创建的数组并打印包含的数据(例如作者,标题,内容等)。作为参考,post对象是这样的:

myposts:Array
(
    [0] => stdClass Object
        (
            [ID] => 1455
            [post_author] => 3
            [post_date] => 2013-03-27 22:16:33
            [post_date_gmt] => 2013-03-27 22:16:33
            [post_content] => Content
            [post_title] => Title
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => title
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2013-03-27 22:16:42
            [post_modified_gmt] => 2013-03-27 22:16:42
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://www.abc.com/wordpress/?p=1455
            [menu_order] => 0
            [post_type] => post
            [post_mime_type] => 
            [comment_count] => 0
            [member_access_visibility] => default
            [filter] => raw
        )

希望这能帮助你解决你的问题!

欢呼

JD

您只需要调用一个get_posts()函数。

创建一个用字符串分隔的所有用户id的字符串。

现在你可以把它和你的订单一起添加到你的参数中:

$args = array(
    'numberposts' => -1,
    'author' => '1,2,3,4,5',
    'orderby' => 'date' // date is the default anyway, so shouldn't matter
);

那么您的get_posts()调用将包含这些作者的所有帖子。

HTH