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
:
<?php
$blogs = get_last_updated(' ', 0, 1);
foreach ($blogs AS $blog);
switch_to_blog($blog["blog_id"]);
$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():
<?php
$blogs = get_last_updated(' ', 0, 1);
foreach ($blogs AS $blog);
switch_to_blog($blog["blog_id"]);
$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>
</div>
<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>
</div>
</div>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>
The
orderby
-parameter should bepost_date
instead of date.your code would look something like this:
Please do not forget to call
restore_current_blog()
in yourforeach
. If you usedswitch_to_blog()
more than once before callingrestore_current_blog()
, it won’t work anymore.@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:
Then, to check it:
References
update_site_option()
get_site_option()
Notes
draft_to_publish
,pending_to_publish
, etc.$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 yourget_site_option()
, you’ve immediately got access to the latest update’s time as well as itsblog_id
andpost_id
.