Ask your WordPress questions! Pay money and get answers fast! (more info)

Multisite Recent Posts - Sort results by Post Date not Blog ID WordPress

  • SOLVED

Hi All,

I have a multi-site installation from which I am pulling the latest post from each site to appear on the homepage.

With the code below I am able to switch to each blog and get posts but the result is sorted in the order of the blog ID (1-7).

I would prefer that the posts to be sorted by post date.

Many Thanks



<!-- START MultiSite POSTS -->
<?php
$original_blog_id = get_current_blog_id(); // get current blog

$bids = array(1,2,3,4,5,6,7); // all the blog_id's to loop through


foreach($bids as $bid):
switch_to_blog($bid); //switched to blog with blog_id $bid

// ... code for each blog ...
$posts = get_posts($bid.'&posts_per_page=1');
foreach($posts as $post){
setup_postdata( $post );
// Start HTML Layout
echo "<article id='foo' class='post loop'>";
echo "<div class='thumb-container'>"; echo the_post_thumbnail();
echo "<div class=\"share\"><a href=\"#\" class=\"share-post\">";
echo _e('Share this post', 'purepress');
echo "</a>";
echo "<a href=\"";
echo the_permalink();
echo "\" class=\"thumb-permalink\">";
echo _e('Permalink', 'purepress');
echo "</a>";
echo "</div>";

echo "<div class=\"share-modal-box\">";
echo "<h3>";
echo _e('Share this post', 'purepress');
echo "</h3>";
echo "</div>"; echo "</div>";


// POST CONTAINER
echo "<div class=\"post-container";
if (!has_post_thumbnail()) { echo "no-thumb";}
echo "\">";

echo"<h2 class=\"entry-title\"><a href=\""; the_permalink(); echo"\">"; the_title();
echo"</a></h2>";

printf('<a href="%1$s" class="published-time" title="%2$s" rel="bookmark">%3$s</a>', get_permalink(), esc_attr(get_the_time()), get_the_date());

// echo get_permalink(), esc_attr(get_the_time()), get_the_date());
// echo" | ";


// Author
echo "<a class=\"author-link\" href=\"";
echo get_author_posts_url(get_the_author_meta( 'ID' ));
echo "\">";
echo the_author_meta('display_name');
echo "</a>";

// Flag GFX
if ($bid == 2 ) { echo '<span><a href="dk/"><img src="flags-dk.png" /></a></span>'; }
elseif ($bid == 3 ) { echo '<span><a href="fi/"><img src="flags-fi.png" /></a></span>'; }
elseif ($bid == 6 ) { echo '<span><a href="nl/"><img src="flags-nl.png" /></a></span>'; }
elseif ($bid == 4 ) { echo '<span><a href="no/"><img src="flags-no.png" /></a></span>'; }
elseif ($bid == 5 ) { echo '<span><a href="sv/"><img src="flags-sv.png" /></a></span>'; }
elseif ($bid == 7 ) { echo '<span><a href="uk/"><img src="flags-uk.png" /></a></span>'; }
elseif ($bid == 1 ) { echo '<span><a href="eu"><img src="flags-eu.png" /></a></span>'; }

// Excerpt
echo "<div class=\"excerpt\">".the_excerpt()."</div>";
echo "</div>";
echo "</article>";
}
endforeach ;

switch_to_blog( $original_blog_id ); //switched back to current blog
?>
<!-- END MultiSite POSTS -->

Answers (2)

2014-05-24

Dbranes answers:

One way is to collect the post objects into an array with published date (in unix time) as the array keys, and then order it with <em>ksort()</em> or <em>krsort()</em> .

Another idea would be to create a custom table and let each blog write to it, via hook when it saves a post. I've sometimes used this approach.

Some plugins just use another blog ( like blog_id=1) to collect posts from other blogs, via hooks.

Here's a code snippet for the first idea:


// All the blog_id's to loop through:
$bids = array(1,2,3,4,5,6,7);

// Collect the latest post for each blog in subnetwork :
$network_posts = array();
foreach( $bids as $bid ) {
switch_to_blog( $bid );
$posts = get_posts( 'posts_per_page=1' );
foreach( $posts as $post ) {
$network_posts[ strval( strtotime( $post->post_date) ) ] = array( 'bid' => $bid, 'post' => $post );
}
restore_current_blog();
}

// Sort the post items:
ksort( $network_posts )

// Reverse sort order
//krsort( $network_posts)

// Loop through the network post items:
foreach( $network_posts as $item ):
switch_to_blog( $item['bid'] ); //switched to blog with blog_id $bid
global $post;
$post = $item['post'];
setup_postdata( $post );

// Start HTML Layout
// ... etc ...

restore_current_blog();
endforeach;


This is untested so it might need some adjustments, but you get the idea.


Dbranes comments:

I updated the answer, to assure the unixtime stamp is a string value.

Here's an example how the <em>$network_posts</em> array would look like:

array (
'1401805662' => array( 'bid' => 1, 'post' => WP_POST_OBJECT ),
'1398695262' => array( 'bid' => 2, 'post' => WP_POST_OBJECT ),
'1404397662' => array( 'bid' => 3, 'post' => WP_POST_OBJECT ),
'1400768862' => array( 'bid' => 4, 'post' => WP_POST_OBJECT ),
'1400941662' => array( 'bid' => 5, 'post' => WP_POST_OBJECT )
)


and then the <em>ksort</em> (or <em>krsort </em>for reverse order ) function will sort the array by the array key (unix time stamp).


EdJames comments:

Hi DBranes,

Thank you very much. I'm happy to say that the code is working (I added a semi-colon after ksort( $network_posts ) to prevent syntax errors) but there is one small glitch - the regional flag images, which were previously chosen by $bid, all default to the last item in the array (flags-uk.png).

I wonder if you could suggest a way to remedy this, with the new code.

// Flag GFX
if ($bid == 2 ) { echo '<span><a href="dk/"><img src="flags-dk.png" /></a></span>'; }
elseif ($bid == 3 ) { echo '<span><a href="fi/"><img src="flags-fi.png" /></a></span>'; }
elseif ($bid == 6 ) { echo '<span><a href="nl/"><img src="flags-nl.png" /></a></span>'; }
elseif ($bid == 4 ) { echo '<span><a href="no/"><img src="flags-no.png" /></a></span>'; }
elseif ($bid == 5 ) { echo '<span><a href="sv/"><img src="flags-sv.png" /></a></span>'; }
elseif ($bid == 7 ) { echo '<span><a href="uk/"><img src="flags-uk.png" /></a></span>'; }
elseif ($bid == 1 ) { echo '<span><a href="eu"><img src="flags-eu.png" /></a></span>'; }


Once again, many thanks for all your help.


Dbranes comments:

Glad to hear it's working.

Try <em>$item['bid']</em> instead of <em>$bid</em> or just set:

$bid = $item['bid'];



ps: you should consider constructing a function to generate the flag code, to simplify things.


EdJames comments:

Excellent. Thank you very much..!

2014-05-24

Romel Apuya answers:

try this

<!-- START MultiSite POSTS -->

<?php

$original_blog_id = get_current_blog_id(); // get current blog



$bids = array(1,2,3,4,5,6,7); // all the blog_id's to loop through





foreach($bids as $bid):

switch_to_blog($bid); //switched to blog with blog_id $bid



// ... code for each blog ...

$posts = get_posts($bid.'&posts_per_page=1');
endforeach ;


usort($posts, get_post_time);

foreach($posts as $post){

setup_postdata( $post );

// Start HTML Layout

echo "<article id='foo' class='post loop'>";

echo "<div class='thumb-container'>"; echo the_post_thumbnail();

echo "<div class=\"share\"><a href=\"#\" class=\"share-post\">";

echo _e('Share this post', 'purepress');

echo "</a>";

echo "<a href=\"";

echo the_permalink();

echo "\" class=\"thumb-permalink\">";

echo _e('Permalink', 'purepress');

echo "</a>";

echo "</div>";



echo "<div class=\"share-modal-box\">";

echo "<h3>";

echo _e('Share this post', 'purepress');

echo "</h3>";

echo "</div>"; echo "</div>";





// POST CONTAINER

echo "<div class=\"post-container";

if (!has_post_thumbnail()) { echo "no-thumb";}

echo "\">";



echo"<h2 class=\"entry-title\"><a href=\""; the_permalink(); echo"\">"; the_title();

echo"</a></h2>";



printf('<a href="%1$s" class="published-time" title="%2$s" rel="bookmark">%3$s</a>', get_permalink(), esc_attr(get_the_time()), get_the_date());



// echo get_permalink(), esc_attr(get_the_time()), get_the_date());

// echo" | ";





// Author

echo "<a class=\"author-link\" href=\"";

echo get_author_posts_url(get_the_author_meta( 'ID' ));

echo "\">";

echo the_author_meta('display_name');

echo "</a>";



// Flag GFX

if ($bid == 2 ) { echo '<span><a href="dk/"><img src="flags-dk.png" /></a></span>'; }

elseif ($bid == 3 ) { echo '<span><a href="fi/"><img src="flags-fi.png" /></a></span>'; }

elseif ($bid == 6 ) { echo '<span><a href="nl/"><img src="flags-nl.png" /></a></span>'; }

elseif ($bid == 4 ) { echo '<span><a href="no/"><img src="flags-no.png" /></a></span>'; }

elseif ($bid == 5 ) { echo '<span><a href="sv/"><img src="flags-sv.png" /></a></span>'; }

elseif ($bid == 7 ) { echo '<span><a href="uk/"><img src="flags-uk.png" /></a></span>'; }

elseif ($bid == 1 ) { echo '<span><a href="eu"><img src="flags-eu.png" /></a></span>'; }



// Excerpt

echo "<div class=\"excerpt\">".the_excerpt()."</div>";

echo "</div>";

echo "</article>";

}





switch_to_blog( $original_blog_id ); //switched back to current blog

?>

<!-- END MultiSite POSTS -->