Grab the Latest Post from Multiple WordPress Blogs

I searched for a solution to this for hours, and ended up adapting some older code I’ve been using on my own website for years. Sometimes the best solution is the simplest one.

If you use WordPress and you want to put some of your posts on your homepage, this is one way to do it.

// The number of posts you want to display
$howMany = 1;
// Connect to the database
// The amazing query. 100 is the number of characters to get from the post body
$posts = $wpdb->get_results ("SELECT `ID`, `post_title`, SUBSTRING_INDEX(`post_content`, ' ', 100) AS `post_excerpt` FROM $wpdb->posts
WHERE `post_status`= \"publish\" ORDER BY 'ID' DESC LIMIT " . $howMany);
// Instead of $wpdb->posts above, you could put the database table's name. This is useful if you have multiple blogs. Put the blog's table prefix followed by _posts, like wp_posts
foreach($posts as $post){
echo '<h2 class="posttitle"><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></h2>';
echo '<p>' . strip_tags($post->post_excerpt);
echo '<br /><a href="' . get_permalink($post->ID) . '">Continue reading <i>' . $post->post_title . '</i>...</a></p>';
} ?>

Here’s another good resource on the topic at scriptygoddess, though I didn’t take the time to fully understand what’s going on in that solution.

By the way, Blueprint is a pretty good CSS framework. Here’s a good tutorial on it, but it needs to be updated to reflect new changes in 0.5. Namely, the number of columns has been increased from 14 to 24, so make sure each row adds up to have 10 more columns than it does in that tutorial.

4 Responses to “Grab the Latest Post from Multiple WordPress Blogs”

  1. Darren says:

    Hi. Thanks for linking to my blog ( I hadn’t realized these changes to blueprint had taken place (I must have forgotten to add bjorky’s blog to my reader). Thanks for alerting me. I have updated the guide to suit the new number of columns. Thanks a lot.

  2. Thanks, this is exactly what I’m looking for.

    My only problem is that I have multiple blogs that I want to grab posts from, but this code only pulls posts from the first section of code. Even when changing this line “require_once(”blog/wp-config.php”);” to another config file, the second section of code displays the same posts as the first section.

    Any ideas?

  3. Peta says:

    Hi! Nice solution!
    But is there a possibility to display images as well? Would be really nice if you would let me know!
    Good work!

  4. peta says:

    well – the easy solution was to just copy and paste the part of code from my wordpress main template where the posting is generated and just add these lines to connect to my homepage above:

    // display your posts (and heading and meta and pictures) on your website

    <div class=”post” id=”post-“>

    <a href=”” rel=”bookmark” title=””>

Leave a Reply