7

The following code gives all posts from the network. What I am trying to achieve :

  • Select which blogs to display (by ID)
  • Select how many post to display (My code selects how many post per blog)
  • Order by date or random

    $blogs = get_last_updated();
    
     foreach ($blogs AS $blog)    {    
    
      switch_to_blog($blog["blog_id"]);
      $lastposts = get_posts('numberposts=3');
    
      foreach($lastposts as $post) : ?>
    
        <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
    
      <?php endforeach;
    
      restore_current_blog();
      }
    
1

I created a plugin which does something similar (called Multisite Post Display https://wordpress.org/plugins/multisite-post-reader/ ) . It displays posts from all multisite sub-sites.

The code in there might be helpful for what you are doing. You are welcome to dig into it and use the code to help with your project. (After all, I used other people's code snippets to develop it.)

I wrote it after I did the Multisite Media Display, since I wanted a way to display media from subsites on one page, and couldn't find any plugin that did that. Both have been useful to monitor posted media and content from my multisite.

Free, open source, and all that. Hope it is helpful.

1

Ricks Answer is surely helpful but I wanted to share my approach, which is an adoption or extension of your code:

First get a list of selected blogs in your network.:

$args = array('site__in' => array(2, 3, 6))
$sitesObj = get_sites($args);
$sites = object_to_array($sitesObj);

You can also exclude sites by using 'site__not_in' in the arguments of get_sites().

Convert the $sitesObj object into an array:

$sites = object_to_array($sitesObj);

object_to_array($object) {
  if (!is_object($object) && !is_array($object)) {
    return $object;
  }
  return array_map('object_to_array', (array) $object) ;
}

Then initialize a counter to control the total of posts to show and switch to each selected blog to fire the loop with your custom arguments:

$postCounter = 0;
$maxPosts = 5; // total number of posts to show

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

  $args = array(
    'post_type' => 'post', // or custom post type
    'posts_per_page' => 2, // number of posts per blog
    'order' => 'DESC',
    'orderby' => 'date' // you could also use 'rand' here
  );

  $loop = new WP_Query($args);

  if ($loop->have_posts()) :
    while ($loop->have_posts() && $counter < $maxPosts) : $loop->the_post();
      // your output
    endwhile;
  endif;

  restore_current_blog();
}

I hope that helps :)

  • No need to run a recursive function to make a site array. Just use switch_to_blog($site->blog_id); instead :) – Zencode.dk Sep 10 '18 at 12:43
0

This

function wolpostcount_shortcode($atts) {

  function object_to_array($object) {
    if (!is_object($object) && !is_array($object)) {
      return $object;
    }
    return array_map('object_to_array', (array) $object) ;
  }

  $args = array('site__in' => array(1,7,8,12,14,15,20,21,22,25,32,33,36,41,42,46,47,48,49));
  $sitesObj = get_sites($args);
  $sites = object_to_array($sitesObj);

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

    $postcount = wp_count_posts('post')->publish;
    $pagecount = wp_count_posts('page')->publish;
     echo 'Posts:'.$postcount.' Pages:'.$pagecount.'<br>';
    $totalpostcount = $totalpostcount + $postcount;
    $totalpagecount = $totalpagecount + $pagecount;
    restore_current_blog();
  }

  echo 'number of posts '.$totalpostcount.'<br>';
  echo 'number of pages '.$totalpagecount.'<br>';
  • Please add some details to your answer instead of dumping code. Thanks! – Christine Cooper Feb 23 '18 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.