{"id":967,"date":"2011-07-31T18:55:26","date_gmt":"2011-07-31T15:55:26","guid":{"rendered":"http:\/\/richardconsulting.ro\/blog\/?p=967"},"modified":"2015-10-03T19:23:05","modified_gmt":"2015-10-03T16:23:05","slug":"wpms_template_latest_network_posts","status":"publish","type":"post","link":"https:\/\/richardconsulting.ro\/blog\/2011\/07\/wpms_template_latest_network_posts\/","title":{"rendered":"WPMS Network Recent Posts Template for Twenty Eleven theme"},"content":{"rendered":"<p>Copy the code below into a new php template file inside the Twenty Eleven theme, then create a blank page using this template. Accesing the page will list the latest posts from all WPMS network sites except the main site.<\/p>\n<pre>< ?php\r\n\/**\r\n * Template Name: WPMS Recent Posts\r\n * Description: A Page Template that display recent posts from all WPMS network\r\n * Author: Richard Vencu\r\n * Template url: http:\/\/richardconsulting.ro\/blog\/WPMS_template_latest_network_posts\/\r\n *\r\n * @package WordPress\r\n * @subpackage Twenty_Eleven\r\n * @since Twenty Eleven 1.0\r\n *\/\r\n\r\nget_header(); \r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/  RETRIEVE SORTED LATEST POSTS FROM WPMS NETWORK (EXCEPT MAIN SITE)  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/*\r\nParameters\r\n==========\r\n\r\n$how_many (integer): how many recent posts are being displayed.\r\n$how_long_days (integer): time frame to choose recent posts from (in days).\r\n$sort_by (string - post_date\/post_modified\/post_title\/comment_count\/): You can short the lattest post by positing date (post_date) or posting update (post_modified).\r\n$sort_order (constant - SORT_ASC or SORT_DESC, default is SORT_DESC\r\n\r\nReturns\r\n======\r\n\r\nArray of blog ID and post ID in ordered form\r\n\r\n*\/\r\n\r\nfunction wpms_latest_post($how_many = 10, $how_long_days = 30, $sort_by = 'post_date', $sort_order = SORT_DESC ) {\r\n\tglobal $wpdb;\r\n\r\n\t\/\/first, gat all PUBLIC blog id\r\n\t$query = \"SELECT blog_id FROM $wpdb->blogs WHERE blog_id != '1' AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'\";\r\n\t$blogs = $wpdb->get_col($wpdb->prepare($query) );\r\n\r\n\t$recentPosts = array();\r\n\r\n\t\/* Build query args, we retrieve the $how_many number from each site since maybe there are sites with zero recent posts *\/\r\n\t$args = array(\r\n\t\t'post_type' \t\t=> 'post',\r\n\t\t'post_status' \t\t=> 'publish',\r\n\t\t'numberposts' \t\t=> $how_many\r\n\r\n\t);\r\n\r\n\tif ( is_array($blogs) ) {\r\n\t\t\/* filter_where - filtering function that will add our where clause to the query *\/\r\n\t\t$filter_where = create_function ('$where','return $where .= \" AND post_date > \\'' . date('Y-m-d', strtotime('-' . $how_long_days . ' days')) . '\\'\";');\r\n\r\n\t\tadd_filter( 'posts_where', $filter_where );\r\n\r\n\t\tforeach ($blogs as $blog) {\r\n\t\t\tswitch_to_blog( absint($blog) );\r\n\r\n\t\t\t$recentQuery = new WP_Query();\r\n\r\n\t\t\t$recentQuery = get_posts( $args );\r\n\r\n\t\t\tforeach ( $recentQuery as $post ) {\r\n\r\n\t\t\t\t$recentPosts[] = array(\r\n\t\t\t\t\t'blog' \t\t=> $blog,\r\n\t\t\t\t\t'post' \t\t=> $post->ID,\r\n\t\t\t\t\t'post_date' \t=> $post->post_date,\r\n\t\t\t\t\t'post_modified'\t=> $post->post_modified,\r\n\t\t\t\t\t'post_title' \t=> $post->post_title,\r\n\t\t\t\t\t'comment_count'\t=> $post->comment_count\r\n\t\t\t\t\t);\r\n\t\t\t}\r\n\r\n\t\t\twp_reset_query();\r\n\r\n\t\t\trestore_current_blog();\r\n\r\n\t\t}\r\n\r\n\t\tremove_filter( 'posts_where', $filter_where );\r\n\r\n\t\t\/* Sort posts by field and trim to the requested number of posts *\/\r\n\t\tforeach ($recentPosts as $key => $row) {\r\n\t\t$sortkey[$key]  = $row[$sort_by];\r\n\t\t}\r\n\r\n\t\tarray_multisort($sortkey, $sort_order, $recentPosts);\r\n\r\n\t\treturn array_slice( $recentPosts, 0, $how_many );\r\n\t}\r\n\r\n}\r\n\r\n?>\r\n\r\n\t\t<div id=\"primary\">\r\n\t\t\t<div id=\"content\" role=\"main\">\r\n\r\n\t\t\t\t< ?php the_post(); ?>\r\n\r\n\t\t\t\t< ?php get_template_part( 'content', 'page' ); ?>\r\n\r\n\t\t\t\t< ?php if ( function_exists('wpms_latest_post') ) {\r\n\t\t\t\t\t$query = wpms_latest_post();\r\n\r\n\t\t\t\t if ( !empty( $query )) { ?>\r\n\r\n\t\t\t\t\t\t\t< ?php \/* Start the Loop *\/ ?>\r\n\t\t\t\t\t\t\t< ?php foreach ( $query as $netpost ) { \r\n\r\n\t\t\t\t\t\t\t\tswitch_to_blog( $netpost['blog'] );\r\n\r\n\t\t\t\t\t\t\t\tquery_posts ( 'p=' . $netpost['post'] );\r\n\r\n\t\t\t\t\t\t\t\tif ( have_posts() ) {\r\n\r\n\t\t\t\t\t\t\t\t\tthe_post();\r\n\r\n\t\t\t\t\t\t\t\t\tget_template_part( 'content', get_post_format() );\r\n\r\n\t\t\t\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\t\t\twp_reset_query();\r\n\r\n\t\t\t\t\t\t\t\trestore_current_blog(); \r\n\r\n\t\t\t\t\t\t\t\t} ?>\r\n\r\n\t\t\t\t\t\t< ?php }\r\n\t\t\t\t\t} ?>\r\n\r\n\t\t\t\t< ?php comments_template( '', true ); ?>\r\n\r\n\t\t\t<\/div><!-- #content -->\r\n\t\t<\/div><!-- #primary -->\r\n\r\n< ?php get_footer(); ?><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Copy the code below into a new php template file inside the Twenty Eleven theme, then create a blank page using this template. Accesing the page will list the latest posts from all WPMS network sites except the main site. < ?php \/** * Template Name: WPMS Recent Posts * Description: A Page Template that [&hellip;]\n<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[123,124],"class_list":["post-967","post","type-post","status-publish","format-standard","hentry","category-wordpress-2","tag-php","tag-twenty-eleven"],"_links":{"self":[{"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/posts\/967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/comments?post=967"}],"version-history":[{"count":2,"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":1296,"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/posts\/967\/revisions\/1296"}],"wp:attachment":[{"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/richardconsulting.ro\/blog\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}