I'm pulling posts from two blogs, the main blog and the current blog. I've pulled all the posts into an array ($hold
) so I can sort them all by date (rather than show all the posts from the main blog ordered by date, then followed by all the posts from the current blog sorted by date).
global $wpdb;
$blogs = $wpdb->get_results( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (1, 6)" );
if ( ! empty ( $blogs ) ) :
// put all posts from each blog into $hold array
$hold = array();
// loop through each blog
foreach ( $blogs as $blog ) :
switch_to_blog( $blog->blog_id );
$args = array(
'category_name' => 'dt2',
'posts_per_page' => 5
);
// get posts from each blog and add to $hold
$q = new WP_query( $args );
if ( $q->have_posts() ) :
while( $q->have_posts() ) :
$q->the_post();
$hold[] = $post;
endwhile;
endif;
wp_reset_query();
restore_current_blog();
endforeach;
// sort all posts from all blogs by post_date
function sort_posts($a, $b)
{
return strtotime($b->post_date) - strtotime($a->post_date);
}
usort($hold, 'sort_posts');
// iterate through all posts in $hold sorted newest to oldest
global $post;
foreach ( $hold as $i => $post ) :
setup_postdata( $post );
get_template_part( 'content/post' );
endforeach;
wp_reset_postdata();
endif;
When I get to the end and I'm going through the $hold
array, I have problems. They seem to stem from the fact that we're not aware of which posts came from which blogs. So I have broken permalinks, missing featured images, etc. for any post that isn't being pulled from the current blog.
Is there a way to either add an indicator to the $hold
array as I'm retrieving the contents of the post, or to otherwise determine what blog the post came from?
I'm very open to suggestions as to how to sort all posts by date, as I think that's what's leading me into this problem.