php - How to display recent global post in wordpress multisite -
i run wordpress multisite on ayp.no , trying figure out way present subsites logos , recent posts blogs, know there wpmudev premium plugin this, hoping there coding myself (well, not myself, @ least ask here , see)..
first, need function sites, @ hand iterate thought array of sites , pull information wp_get_recent_posts()
(which customized version of get_posts()
).
use following must use plugin, function b5f_print_recent_posts()
available throughout network:
<?php /** * plugin name: recent network posts * plugin uri: http://stackoverflow.com/q/23713801/1287812 * description: creates function lists recent posts sites of network. call in plugins or themes. * author: brasofilo */ /** * iterates throught sites of network , grab recent posts */ function b5f_print_recent_posts() { $blogs = b5f_get_blog_list( 0, 'all', true ); $current_blog_id = get_current_blog_id(); foreach( $blogs $blog ) { switch_to_blog( $blog[ 'blog_id' ] ); echo '<h3>' . $blog['name'] . ' - ' . $blog['domain'] . ' - ' . $blog['desc'] . '</h3>'; $posts = wp_get_recent_posts( array(), object ); if( $posts ) { foreach( $posts $post ) { printf( '- <a href="%s">%s</a><br />', get_permalink( $post->id ), $post->post_title ); } } } switch_to_blog( $current_blog_id ); } /** * returns array of arrays containing information each public blog * hosted on wpmu install. * * blogs marked public , flagged safe (mature flag off) returned. * * @author frank bueltge * * @param integer first blog return in array. * @param integer number of blogs return in array (thus size of array). * setting string 'all' returns blogs $start * @param boolean postcount each blog, default false better performance * @param integer time until expiration in seconds, default 86400s (1day) * @return array returns array of arrays each representing blog. * details represented in following format: * blog_id (integer) id of blog detailed. * domain (string) domain used access blog. * path (string) path used access blog. * postcount (integer) number of posts in blog. * name (string) blog name. * desc (string) blog description. */ function b5f_get_blog_list( $start = 0, $num = 10, $details = false, $expires = 86400 ) { // blog list cache $blogs = get_site_transient( 'multisite_blog_list' ); // debugging purpose if ( defined( 'wp_debug' ) && wp_debug ) $blogs = false; if ( false === $blogs ) { global $wpdb; // add limit select if ( 'all' === $num ) $limit = ''; else $limit = "limit $start, $num"; $blogs = $wpdb->get_results( $wpdb->prepare( " select blog_id, domain, path $wpdb->blogs site_id = %d , public = '1' , archived = '0' , mature = '0' , spam = '0' , deleted = '0' order registered asc $limit ", $wpdb->siteid ), array_a ); // set transient cache set_site_transient( 'multisite_blog_list', $blogs, $expires ); } // if usable, set via var if ( true === $details ) { $blog_list = get_site_transient( 'multisite_blog_list_details' ); // debugging purpose if ( defined( 'wp_debug' ) && wp_debug ) $blog_list = false; if ( false === $blog_list ) { global $wpdb; $current_blog_id = get_current_blog_id(); foreach ( (array) $blogs $details ) { $blog_list[ $details['blog_id'] ] = $details; $blog_list[ $details['blog_id'] ]['postcount'] = $wpdb->get_var( " select count(id) " . $wpdb->get_blog_prefix( $details['blog_id'] ). "posts post_status='publish' , post_type='page'" ); switch_to_blog( $details['blog_id'] ); $blog_list[ $details['blog_id'] ]['name'] = get_blog_details()->blogname; $blog_list[ $details['blog_id'] ]['desc'] = get_bloginfo( 'description' ); } switch_to_blog( $current_blog_id ); // set transient cache set_site_transient( 'multisite_blog_list_details', $blog_list, $expires ); } unset( $blogs ); $blogs = $blog_list; } if ( false === is_array( $blogs ) ) return array(); return $blogs; }
you can add following network dashboard widget in previous plugin test out:
add_action( 'wp_network_dashboard_setup', 'dashboard_setup_so_23713801' ); function dashboard_setup_so_23713801() { wp_add_dashboard_widget( 'widget_so_23713801', __( 'test widget' ), 'print_widget_so_23713801' ); } function print_widget_so_23713801() { b5f_print_recent_posts(); }
Comments
Post a Comment