List posts from all blogs in WordPress multisite

List posts from all blogs in WordPress multisite

27.12.2012

This was one annooying thing to do. It is hard to find any information about how to list posts from all blogs in WordPress multisite. Most websites just refer to a plugin that costs at least $19 a month. I did not want to use that plugin. And I want to do it in the template, so that I can customize the output in the most seo-friendly way, so I had to Google around a little, do some trial and error, and this is now one way to do it:

List posts from all blogs in WordPress multisite installation

The code:

<?php
$blogs = get_last_updated();
echo
<h1>Last blog posts in all blogs</h1>
‘;
foreach ($blogs AS $blog) {
echo
<h2><a href=’http://”.$blog[“domain”].$blog[“path”].“‘>”.get_blog_option( $blog[ ‘blog_id’ ], ‘blogname’ ).“</a></h2>
“;
switch_to_blog($blog[“blog_id”]);
$lastposts = get_posts(‘numberposts=1’);
foreach($lastposts as $post) :
setup_postdata($post);
?>
<a href=”<?php the_permalink(); ?>“><?php the_title(); ?></a>
<?php
endforeach;
restore_current_blog();
}
?>

This code will do some magic. It will print the blogs title with a link to the blog front page (in the h2), and under it a link to the last blog post in that blog, with the blog post title as the anchor text. This code can be developed a lot more, but this is a start!

Note!

This code for listing posts from all blogs in a WordPress multisite installation only works for a multisite where yuo have blogs as subfolders. If you have them as subdomains, you have to modify the code a little. ($blog[“path”] and $blog[“domain”] need to switch positions in a subdomain installation)

I hope it helped you. It would have helped me if this code were present when I searched for it.

Advertising

Download new responsive WordPress template

New responsive WordPress template with big call-to-action-areas, header-slider and different color-themes. It is built on Bootstrap and named Strap Them Boots On. Read more and download!

The one writing here:

Important information

This is the search engine optimization (SEO) and web tactics webiste/blog for Jonathan Björkskog, working at and co-owning Genero Digital Agency, a digital advertising agency based in Helsinki, Finland. If you searched for my Swedish blog, you find it here: jonathanbjorkskog.se.

That's one of my passions.

If you were searching for the other one, kitesurfing, you should check out my kitesurfing blog instead.

Follow on Facebook

Head over to the wall and send me all your questions about Facebook, SEO, or any other awesome topic on the internet!

Premium SEO-Plugin

Brian D. Hawkins about SEOPressor:

It’s almost as if SEOPressor took those features from my checklist. I guess I’m old school but I have a checklist that I use when writing new posts. It’s pretty cool that SEOPressor helps automate some of those tasks and the check/remind feature is very nice. As usual I love your presentation.

http://hotblogtips.com/about

Click Here to Download
SEOPressor
Read my SEOPressor review