WPMS Network Recent Posts Template for Twenty Eleven theme

Copy the code below into a new php template file inside the Twenty Eleven theme, then create a blank page using this template. Accesing the page will list the latest posts from all WPMS network sites except the main site.

< ?php
/**
 * Template Name: WPMS Recent Posts
 * Description: A Page Template that display recent posts from all WPMS network
 * Author: Richard Vencu
 * Template url: https://richardconsulting.ro/blog/WPMS_template_latest_network_posts/
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */

get_header(); 

/////////////  RETRIEVE SORTED LATEST POSTS FROM WPMS NETWORK (EXCEPT MAIN SITE)  /////////////////////
/*
Parameters
==========

$how_many (integer): how many recent posts are being displayed.
$how_long_days (integer): time frame to choose recent posts from (in days).
$sort_by (string - post_date/post_modified/post_title/comment_count/): You can short the lattest post by positing date (post_date) or posting update (post_modified).
$sort_order (constant - SORT_ASC or SORT_DESC, default is SORT_DESC

Returns
======

Array of blog ID and post ID in ordered form

*/

function wpms_latest_post($how_many = 10, $how_long_days = 30, $sort_by = 'post_date', $sort_order = SORT_DESC ) {
	global $wpdb;

	//first, gat all PUBLIC blog id
	$query = "SELECT blog_id FROM $wpdb->blogs WHERE blog_id != '1' AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'";
	$blogs = $wpdb->get_col($wpdb->prepare($query) );

	$recentPosts = array();

	/* Build query args, we retrieve the $how_many number from each site since maybe there are sites with zero recent posts */
	$args = array(
		'post_type' 		=> 'post',
		'post_status' 		=> 'publish',
		'numberposts' 		=> $how_many

	);

	if ( is_array($blogs) ) {
		/* filter_where - filtering function that will add our where clause to the query */
		$filter_where = create_function ('$where','return $where .= " AND post_date > \'' . date('Y-m-d', strtotime('-' . $how_long_days . ' days')) . '\'";');

		add_filter( 'posts_where', $filter_where );

		foreach ($blogs as $blog) {
			switch_to_blog( absint($blog) );

			$recentQuery = new WP_Query();

			$recentQuery = get_posts( $args );

			foreach ( $recentQuery as $post ) {

				$recentPosts[] = array(
					'blog' 		=> $blog,
					'post' 		=> $post->ID,
					'post_date' 	=> $post->post_date,
					'post_modified'	=> $post->post_modified,
					'post_title' 	=> $post->post_title,
					'comment_count'	=> $post->comment_count
					);
			}

			wp_reset_query();

			restore_current_blog();

		}

		remove_filter( 'posts_where', $filter_where );

		/* Sort posts by field and trim to the requested number of posts */
		foreach ($recentPosts as $key => $row) {
		$sortkey[$key]  = $row[$sort_by];
		}

		array_multisort($sortkey, $sort_order, $recentPosts);

		return array_slice( $recentPosts, 0, $how_many );
	}

}

?>

		
< ?php the_post(); ?> < ?php get_template_part( 'content', 'page' ); ?> < ?php if ( function_exists('wpms_latest_post') ) { $query = wpms_latest_post(); if ( !empty( $query )) { ?> < ?php /* Start the Loop */ ?> < ?php foreach ( $query as $netpost ) { switch_to_blog( $netpost['blog'] ); query_posts ( 'p=' . $netpost['post'] ); if ( have_posts() ) { the_post(); get_template_part( 'content', get_post_format() ); } wp_reset_query(); restore_current_blog(); } ?> < ?php } } ?> < ?php comments_template( '', true ); ?>
< ?php get_footer(); ?>

By Richard Vencu

http://www.linkedin.com/in/richardvencu

Leave a comment

Your email address will not be published.