Skip to content

Instantly share code, notes, and snippets.

@pdewouters
Created October 2, 2014 16:50
Show Gist options
  • Select an option

  • Save pdewouters/ceeea2e4ee7f82735d33 to your computer and use it in GitHub Desktop.

Select an option

Save pdewouters/ceeea2e4ee7f82735d33 to your computer and use it in GitHub Desktop.
<?php
global $wpdb;
$results = get_transient( 'hub_listed_blogs' );
if ( false === $results ) {
$results = $wpdb->get_results( 'SELECT blog_id, domain FROM wp_blogs', ARRAY_A );
// cache for a week
set_transient( 'hub_listed_blogs', 2 * WEEK_IN_SECONDS );
}
if ( 0 === count( $results ) ) {
return;
}
$latest_posts = get_transient( 'hub_latest_posts' );
if ( false === $latest_posts ) {
foreach ( $results as $blog ) {
switch_to_blog( $blog['blog_id'] );
$latest_posts[] = hmn_get_latest_post( $blog['blog_id'] );
set_transient( 'hub_latest_posts', 1 * HOUR_IN_SECONDS );
}
}
// Return the latest post for a blog
function hmn_get_latest_post() {
$args = array(
'posts_per_page' => 1,
'ignore_sticky_posts' => true,
);
$latest_posts = get_posts( $args );
wp_reset_postdata();
if ( 0 < count( $latest_posts ) ) {
return $latest_posts[0];
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment