/** * [mn_network_posts count="6"] * * Aggregates the most recent posts from all public subsites in the Mulembe * Nation network and renders them as a grid matching .mn-posts-section CSS. * * Uses a 5-minute transient to avoid switch_to_blog() storms on every page load. * * Added: 2026-05-22 — Task 7 (fix network post loop on main site) */ add_shortcode( 'mn_network_posts', function( $atts ) { $atts = shortcode_atts( [ 'count' => 6 ], $atts ); $count = max( 1, min( 30, intval( $atts['count'] ) ) ); $cache_key = 'mn_network_posts_' . $count; $cached = get_transient( $cache_key ); if ( $cached !== false ) { return $cached; } // Public subsites (blog IDs from multisite registration) // 1 = main aggregator (skip), 2=bukusu, 4=food, 5=politics, 6=business, // 7=sport, 9=travel. 3=enyanga and 8=findyourfamily are private. $blog_ids = [ 2, 4, 5, 6, 7, 9 ]; $all_posts = []; foreach ( $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); $posts = get_posts( [ 'numberposts' => $count, 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', ] ); foreach ( $posts as $post ) { $all_posts[] = [ 'date' => strtotime( $post->post_date_gmt ), 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'excerpt' => wp_trim_words( get_the_excerpt( $post ), 15 ), 'thumb' => get_the_post_thumbnail_url( $post, 'medium' ), 'blog_id' => $blog_id, 'blog_name' => get_bloginfo( 'name' ), ]; } restore_current_blog(); } // Merge: sort by date descending, then slice usort( $all_posts, function( $a, $b ) { return $b['date'] - $a['date']; } ); $all_posts = array_slice( $all_posts, 0, $count ); if ( empty( $all_posts ) ) { return '

No recent posts from the network.

'; } // Render matching wp-block-post-template grid $out = ''; set_transient( $cache_key, $out, 5 * MINUTE_IN_SECONDS ); return $out; }); Vihiga Sub-Counties Archives | Mulembe Nation