** * List recent posts across a Multisite network * * @uses get_blog_list(), get_blog_permalink() * * @param int $size The number of results to retrieve * @param int $expires Seconds until the transient cache expires * @return object Contains the blog_id, post_id, post_date and post_title */ function wp_recent_across_network( $size = 10, $expire_post = 7200, $exclude_home = true ) { if( !is_multisite() ) return false; // Cache the results with the WordPress Transients API // Get any existing copy of our transient data delete_site_transient( 'recent_across_network' ); if ( ( $recent_across_network = get_site_transient( 'recent_across_network' ) ) === false ) { // No transient found, regenerate the data and save a new transient // Prepare the SQL query with $wpdb global $wpdb; $base_prefix = $wpdb->get_blog_prefix(0); $base_prefix = str_replace( '1_', '' , $base_prefix ); // No loop necessary, Sitewide Tags makes it easy to snag latest posts from // the main site (side with blog_id = 1) where they also have permalinks $limit = absint($size); $query = ""; $posts_table = $base_prefix . "posts"; $posts_table = esc_sql( $posts_table ); $blogs_table = esc_sql( $base_prefix . 'blogs' ); $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table\n"; $query .= "\tWHERE $posts_table.post_type = 'post'\n"; $query .= "\tAND $posts_table.post_status = 'publish'\n"; $query .= "\tAND $blogs_table.blog_id = 1 )\n"; // Works with Sitewide Tags! $query .= "ORDER BY post_date DESC LIMIT 0, $limit"; // Run the query $recent_across_network = $wpdb->get_results( $query ); // Set the Transients cache to expire every two hours set_site_transient( 'recent_across_network', $recent_across_network, $expire_post ); } // Format the HTML output $html = ''; return $html;