按日期对来自多个站点的帖子进行排序

时间:2016-08-12 作者:jrnd.bjrkhg

我有一个WordPress多站点安装,在那里我需要将所有帖子放到主站点的一个提要中。我尝试过使用SQL查询,但这阻止了我访问大量所需的数据。我现在有了一种不同的方法,但我找不到按日期对所有不同帖子进行排序的方法。

foreach( $sites as $site ){
  switch_to_blog( $site[\'blog_id\'] );

    $allBlogPosts = get_posts( array(
      \'posts_per_page\'   => -1,
      \'orderby\'          => \'date\',
        \'order\'            => \'DESC\'
    ));

    foreach( $allBlogPosts as $post ){
      setup_postdata( $post ); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="post__header">
          <?php the_post_thumbnail(); ?>
          <?php if(!is_single()) { ?>
            <time class="timeStamp"><span class="timeStamp__month"><?php the_time("d"); ?> </span> <span class="timeStamp__day"> <?php the_time("F"); ?> </span> </time>
          <?php } ?>
          <?php
            if ( is_single() ) {
              the_title( \'<h1 class="post__title">\', \'</h1>\' );
            } else {
              the_title( \'<h2 class="post__title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></h2>\' );
            }

          if ( \'post\' === get_post_type() && is_single() ) : ?>
          <div class="post__meta">
            <?php themway_posted_on(); ?>
          </div><!-- .post__meta -->
          <?php
          endif; ?>
        </header><!-- .post__header -->
        <div class="post__content">
          <?php
            the_content( sprintf(
              /* translators: %s: Name of current post. */
              wp_kses( __( \'Continue reading %s <span class="meta-nav">&rarr;</span>\', \'themway\' ), array( \'span\' => array( \'class\' => array() ) ) ),
              the_title( \'<span class="screen-reader-text">"\', \'"</span>\', false )
            ) );

            wp_link_pages( array(
              \'before\' => \'<div class="page-links">\' . esc_html__( \'Pages:\', \'themway\' ),
              \'after\'  => \'</div>\',
            ) );
          ?>
        </div><!-- .post__content -->
      </article><!-- post -->
      <?php
      wp_reset_postdata();
    }// end foreach for $skiPost as $post

    restore_current_blog();
  }// end foreach for $sites as $site
所以现在它是按博客排序的,所以它适合每个博客,但我需要将所有内容合并到一个漂亮的大提要中。

1 个回复
最合适的回答,由SO网友:TheGentleman 整理而成

你可以usort 迭代之前的数组。

$allBlogPosts = get_posts( array(
  \'posts_per_page\'   => -1,
  \'orderby\'          => \'date\',
    \'order\'            => \'DESC\'
));

usort($allBlogPosts, \'sortPosts\');    

function sortPosts($a, $b){
    return strtotime($a->post_date) - strtotime($b->post_date);
}

//Your display loop...
仅供参考,如果您使用的PHP>=5.3,您可以使用closure, 我个人认为这类东西更干净。

编辑:上述方法不起作用的原因是我误读了你的代码,没有意识到$allBlogPosts array一次中的博客数量永远不会超过1篇。这应该可以:

$allPosts = [];
foreach( $sites as $site ){
  switch_to_blog( $site[\'blog_id\'] );

    $allBlogPosts = get_posts( array(
      \'posts_per_page\'   => -1,
      \'orderby\'          => \'date\',
        \'order\'            => \'DESC\'
    ));

    foreach( $allBlogPosts as $post ){
      ob_start();
      setup_postdata( $post ); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="post__header">
          <?php the_post_thumbnail(); ?>
          <?php if(!is_single()) { ?>
            <time class="timeStamp"><span class="timeStamp__month"><?php the_time("d"); ?> </span> <span class="timeStamp__day"> <?php the_time("F"); ?> </span> </time>
          <?php } ?>
          <?php
            if ( is_single() ) {
              the_title( \'<h1 class="post__title">\', \'</h1>\' );
            } else {
              the_title( \'<h2 class="post__title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></h2>\' );
            }

          if ( \'post\' === get_post_type() && is_single() ) : ?>
          <div class="post__meta">
            <?php themway_posted_on(); ?>
          </div><!-- .post__meta -->
          <?php
          endif; ?>
        </header><!-- .post__header -->
        <div class="post__content">
          <?php
            the_content( sprintf(
              /* translators: %s: Name of current post. */
              wp_kses( __( \'Continue reading %s <span class="meta-nav">&rarr;</span>\', \'themway\' ), array( \'span\' => array( \'class\' => array() ) ) ),
              the_title( \'<span class="screen-reader-text">"\', \'"</span>\', false )
            ) );

            wp_link_pages( array(
              \'before\' => \'<div class="page-links">\' . esc_html__( \'Pages:\', \'themway\' ),
              \'after\'  => \'</div>\',
            ) );
          ?>
        </div><!-- .post__content -->
      </article><!-- post -->
      <?php
      $allPosts[] = [\'display\'=>ob_get_clean(), \'post_date\'=>get_the_time(\'U\')];
      wp_reset_postdata();
    }// end foreach for $skiPost as $post

    restore_current_blog();
  }// end foreach for $sites as $site        

  function sortPosts($a, $b){
    return $a[\'post_date\'] - $b[\'post_date\'];
  }
  usort($allPosts, \'sortPosts\'); 
  foreach($allPosts as $p) echo $p[\'display\'];

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果