Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active October 9, 2022 18:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/5718743 to your computer and use it in GitHub Desktop.
Save mhulse/5718743 to your computer and use it in GitHub Desktop.
<?php
/**
* Get a list of the most recently updated blogs.
*
* A copy of WP's `get_last_updated()`, exept I've added ability to filter
* and removed the need to pass so many args.
*
* @todo Move `$ignore` values to more global location?
*
* @see https://gist.github.com/mhulse/5718743
* @see http://wpseek.com/get_last_updated/
* @see http://codex.wordpress.org/WPMU_Functions/get_last_updated
* @see http://wordpress-hackers.1065353.n5.nabble.com/WP-3-5-2-multisite-How-to-use-NOT-IN-in-wpdb-prepare-tp41812.html
*/
function FOO_get_last_updated($quantity = 40, $start = 0, $ignore = array('1', '19', '21',)) {
global $wpdb;
$ignore = implode(', ', array_map('absint', $ignore));
return $wpdb->get_results($wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND blog_id NOT IN ($ignore) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity), ARRAY_A);
}
//--------------------------------------------------------------------
/**
* Get latest posts from multisite blogs.
*
* @todo Move `$ignore` values to more global location?
*
* @see http://wordpress.stackexchange.com/q/5001/32387
* @see http://wordpress.stackexchange.com/a/49027/32387
* @see https://gist.github.com/mhulse/5718743
* @see http://snipplr.com/view/65413/
* @see http://wordpress-hackers.1065353.n5.nabble.com/WP-3-5-2-multisite-How-to-use-NOT-IN-in-wpdb-prepare-tp41812.html
*/
function FOO_recent_ms_posts($count = 10, $ignore = array('1', '19', '21',)) {
global $wpdb, $table_prefix;
$ignore = implode(', ', array_map('absint', $ignore));
$rows = NULL;
$tables = array();
$query = '';
$i = 0;
$posts = array();
$post = NULL;
$rows = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE blog_id NOT IN ($ignore) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'", 0), ARRAY_A);
if ($rows) {
foreach ($rows as $row) {
$tables[$row['blog_id']] = $wpdb->get_blog_prefix($row['blog_id']) . 'posts';
}
if (count($tables)) {
foreach ($tables as $blog_id => $table) {
if ($i) {
$query .= ' UNION ';
}
$query .= " (SELECT ID, post_date, $blog_id as `blog_id` FROM $table WHERE post_status = 'publish' AND post_type = 'post')";
$i++;
}
$query .= " ORDER BY post_date DESC LIMIT 0, $count;";
$rows = $wpdb->get_results($query);
if ($rows) {
foreach ($rows as $row) {
$post = get_blog_post($row->blog_id, $row->ID);
$post->blog_id = $row->blog_id;
$post->row_id =$row->ID;
$post->permalink = get_blog_permalink($row->blog_id, $row->ID);
$posts[] = $post;
}
return $posts;
}
}
}
}
<?php foreach (FOO_get_last_updated() as $blog): ?>
<?php switch_to_blog($blog['blog_id'], true); ?>
<?php $details = get_blog_details($blog['blog_id']); ?>
<section>
<header>
<hgroup>
<div class="streamer alt2">
<h1><a href="http://blogs.registerguard.com/">Blogs</a></h1>
<h2><a href="<?=$details->siteurl?>"><?=$details->blogname?></a></h2>
</div> <!-- /.streamer -->
</hgroup>
</header>
<div class="w_row">
<div class="w_col w_C130">
<?=get_template_part('includes/blog', 'info')?>
</div> <!-- /.w_col -->
<?php global $post; ?>
<div class="w_col w_C470 w_D300 Dla">
<article>
<?php $latest_post = get_posts('numberposts=1'); ?>
<?php foreach ($latest_post as $post): ?>
<?php setup_postdata($post); ?>
<header>
<p class="pub"><time pubdate datetime="<?=get_iso_8601()?>"><?=the_time('g:i a, M j')?></time>
<h1 class="h4"><a href="<?=the_permalink()?>"><?=the_title()?></a></h1>
</header>
<p><?php the_excerpt(); ?><?=edit_post_link('edit', ' [', ']')?></p>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</article>
</div> <!-- /.w_col -->
<div class="w_col w_D470 w_Cc Dlb">
<?php $latest_posts = get_posts('numberposts=6&offset=1'); ?>
<ul class="li1">
<?php foreach($latest_posts as $post): ?>
<?php setup_postdata($post); ?>
<li><a href="<?=the_permalink()?>"><?=the_title()?></a><?=edit_post_link('edit', ' [', ']')?></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
</div> <!-- /.w_col -->
</div> <!-- /.w_row -->
<footer>
<p class="jump"><a href="<?=$details->siteurl?>">More <span><?=$details->blogname?></span> &raquo;</a></p>
</footer>
</section>
<?php restore_current_blog(); ?>
<?php endforeach; ?>
<?php # Most recent posts from all blogs: ?>
<?php $posts = FOO_recent_ms_posts(10); ?>
<?php foreach ($posts as $post): ?>
<li><span><a href="<?=get_blog_details($post->blog_id)->siteurl?>"><?=get_blog_details($post->blog_id)->blogname?>:</a></span> <a href="<?=$post->permalink?>"><?=$post->post_title?></a></li>
<?php endforeach; ?>
@brisbanebighead
Copy link

Hi I'm new. Is there any new version of these codes for the latest WP and PHP?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment