Friday, April 17, 2015

Get last published posts in your entire WordPress Multisite network

To display last published posts from the entire network on the main blog of your WordPress Multisite, you can use this plugin I wrote (*) yesterday:

Github repo: https://github.com/dangtrinh/WordPress_Multisites_Utilities_Plugin

1. Install the WordPress Multisite Utilities Plugin:

$ cd /var/www/your_wordpress_installation/wp-content/plugins
$ git clone https://github.com/dangtrinh/WordPress_Multisites_Utilities_Plugin

2. Activate the plugin for your network (network only)

3. In your home page template file (for example page-home.php), call the get_recent_network_posts function to get latest posts (6 posts by default) from your network. For example:

<?php
$lastposts = get_recent_network_posts();
foreach( $lastposts as $post):
?>
<div class="col-1-3">
<div class="blog-post">
<div class="blog-post-thumbnail" style="background-image: url('<?php echo $post['thumb_url'];?>')"></div>
<a href="<?php echo $post['permalink']; ?>">
<div class="blog-post-content">
<div class="content-container">
<p class="date"><?php echo strftime("%m/%d/%Y", strtotime($post['the_post']->post_date)); ?></p>
<h4><?php echo $post['the_post']->post_title; ?></h4>
<p><?php $content = $post['the_post']->post_content; $trimmed_content = wp_trim_words( $content, 15, '...' ); echo $trimmed_content; ?></p>
</div>
</div>
</a>
</div>
</div>
<?php 
endforeach;
?>

Notes: 

1. The return data is an array of objects:

$posts = Array(

[0] => Array(
               ['the_post']      => <the post object>
               ['thumb_url']   => <url of the featured image>
               ['permalink']   => <permanent link of the post>

[1] => Array(...)

...

2. This is my first WordPress plugin ever!!! :D


Reference: Most of the code in the plugin I borrowed around the Internet, modified and packed everything into one plugin:

[0] https://gist.github.com/bamadesigner/f5a338805d28f9c15df7
[1] http://snipplr.com/view/65413/recent-posts-from-all-sites-wordpress-multisite/