• What is the latest, best solution to get recent posts from across a multisite network on your central home page?

    The network-latest-posts plugin is not a solution; it requires you give it blog ID’s from the blogs in your network.

    I am looking for an aggregator that automatically collects the latest posts from dozens, maybe hundreds of sites, without killing the server.

    The solution should probably use a combination of wp_get_sites() with get_last_updated().

    This proof-of-concept snippet is floating around:

    <?
    $blogs = get_last_updated();
    echo '
    <h1>Last posts in network</h1>
    ';
    foreach ($blogs AS $blog) {
    	echo "
    <h2>".$blog["domain"].$blog["path"]."</h2>
    ";
    	switch_to_blog($blog["blog_id"]);
    		$lastposts = get_posts('numberposts=1');
    		foreach($lastposts as $post) :
    			setup_postdata($post);
    			the_title();
    		endforeach;
    	restore_current_blog();
    }
    ?>

    This one from 2011 has some kind of solution, but it is producing an annoying syntax error and I can’t figure out how to fix it:

    http://www.smashingmagazine.com/2011/11/wordpress-multisite-practical-functions-methods/

    So what is the latest? Has anyone else worked on this? Can someone put this together, point me in the right direction?

    I have another old multisite network latest posts aggregator script that I could post, but it looks very messy.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m currently using the following on a small project. This is a controlled project with only a few sites and this approach probably wouldn’t be ideal for a larger multisite network

    function as_latest_posts() {
    
    	$as_latest_posts = array();
    
    	$args = array(
    		'offset' 	=> 1
    	);
    
    	$blogs = wp_get_sites( $args );
    
    	// Get the 5 latest posts from each blog
    	foreach( $blogs as $blog ) {
    
    		switch_to_blog( $blog['blog_id'] );
    
    			$args = array(
    				'post_type' 		=> 'post',
    				'no_found_rows' 	=> true,
    				'posts_per_page' 	=> 5
    			);
    
    			$as_posts = new WP_Query( $args );
    
    			if( $as_posts->have_posts() ) {
    				while( $as_posts->have_posts() ) {
    					$as_posts->the_post();
    
    						global $post;
    
    						$as_latest_posts[] = $post;
    				}
    			}
    
    			wp_reset_postdata();
    
    		restore_current_blog();
    	}
    
    	// Get all the posts returned and sort them based on post date
    	$sortPosts = array();
    
    	foreach( $as_latest_posts as $latest_post ) {
    	    foreach( $latest_post as $key=>$value ){
    
    	        if(!isset($sortPosts[$key])){
    	            $sortPosts[$key] = array();
    	        }
    	        $sortPosts[$key][] = $value;
    
    	    }
    	}
    
    	$orderby = 'post_date';
    
    	array_multisort( $sortPosts[$orderby], SORT_DESC, $as_latest_posts );
    
    	// Limit the results to the first 5 from the array
    	$as_latest_posts = array_slice( $as_latest_posts, 0,5 );
    
    	return $as_latest_posts;
    
    }

    I am also trying to accomplish the same thing – but have no clue how to implement your code – any help?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get latest posts from all sites across multisite network – 2016’ is closed to new replies.