I have to get the last post from my Multisite network on WordPress. For now I use this code for display the last updated post after a cycle on each blog_id:


$blogs = get_last_updated(' ', 0, 1);
foreach ($blogs AS $blog);
        $lastposts = get_posts('numberposts=1&orderby=date');
        foreach($lastposts as $post) : setup_postdata($post);?>

But if I want to get the last post -- not the last post updated -- how can I do that? Because if I change and refresh a post I get the post like the last on main page. But this is not the real last post.

Update - This is the full version, i have also used the restore_current_blog():


    $blogs = get_last_updated(' ', 0, 1);
    foreach ($blogs AS $blog);
            $lastposts = get_posts('numberposts=1&orderby=date');
            foreach($lastposts as $post) : setup_postdata($post);?> 
    <div class="container-img">
     <a class="anteprima_princ" href="<?php echo get_page_link($post->ID); ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_post_thumbnail('immagine-principale'); ?></a>
                <h2 class="entrytitlepost"><a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php the_title(); ?></a></h2>

                  <div class="post-content-princ">
        <p><?php the_content_rss('...', FALSE, '', 40); ?></p>
        <div id="lt">
        <div id="leggitutto"><div id="croce"><div id="alto"></div><div id="largo"></div></div><a class="lt" href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>">LEGGI TUTTO</a></div>

         <?php endforeach ; ?>

                        <?php restore_current_blog(); //switched back to main site ?>
  • You might consider using update_site_option() to set a site option when a new post is added. You should be able to hook it to the appropriate Post Status Transition (perhaps new_to_publish). That way you don't have to do a whole pile of switch_to_blog() / restore_current_blog() calls -- your post's info (ie, ID) can be stored in the site option. – Pat J Feb 4 '14 at 21:50
  • @PatJ thank you for your advice, can you show me your version of code for fix mine? – Giulio Bambini Feb 4 '14 at 22:16
  • What exactly does not work with the solution I posted? Have you tried just editing the orderby? – fischi Feb 4 '14 at 22:23
  • I have only edited the orderby and if i save like this i see white page... – Giulio Bambini Feb 4 '14 at 22:41
  • My mistake, miss a ); . But still doesn't work, because when i refresh a post it became the last post, but this is not really the last post. Is the last updated. – Giulio Bambini Feb 4 '14 at 22:52

The orderby-parameter should be post_date instead of date.

your code would look something like this:

$blogs = get_last_updated(' ', 0, 1);
foreach ($blogs AS $blog) {
    $args = array(
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'numberposts'     => 1,
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'suppress_filters' => true
    $lastposts = get_posts( $args );

    foreach($lastposts as $thispost) {



Please do not forget to call restore_current_blog() in your foreach. If you used switch_to_blog() more than once before calling restore_current_blog(), it won't work anymore.

  • +1 for the restore_current_blog() reminder. – Pat J Feb 4 '14 at 21:53
  • 1
    @PatJ this should be the standard footer of every multisite answer :D – fischi Feb 4 '14 at 21:55
  • I have tried to readapt your code in my case, but seems not working. I upload the full code so you can see more clearly what is my problem ok? – Giulio Bambini Feb 4 '14 at 21:58
  • @fischi i have update my question. I have included the rest of html and php. – Giulio Bambini Feb 4 '14 at 22:08
  • Did you try deactivating all your plugins? Maybe one of them interferes with your Query. – fischi Feb 5 '14 at 15:30

@fischi's answer appears to answer the question you asked; I suggest this only as an alternative.

You can create/update a site option every time a new post is published:

add_action( 'new_to_publish', 'wpse133433_latest_post' );
function wpse_133433_latest_post( $post ) {
    $blog_id = get_current_blog_id();
    $latest = array( 
        'blog_id' => $blog_id,
        'post_id' => $post->ID,
    update_site_option( 'latest_post_in_network', $latest );

Then, to check it:

$latest = get_site_option( 'latest_post_in_network' );
// should return an array with blog_id and post_id



  • You need the first code block to run on every site in your network (that you want to collect posts from) -- you can make it into a Must Use plugin or a network-enabled plugin.
  • You may need to hook to more than one Post Transition hook -- ie, draft_to_publish, pending_to_publish, etc.
  • The $latest array in the first code block can hold more info, too. You could, for example, add the current time, so that when you do your get_site_option(), you've immediately got access to the latest update's time as well as its blog_id and post_id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.