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

Display all posts ordered by date in wordpress multisite WordPress

  • REFUNDED

Hi
I have a wordpress multisite which the main site homepage, archive and search is displaying
the latest posts from all inner blogs using the very simple function:
get_last_updated();

Here is a light version of the code that I am re-using throught the site:

<?php
$blogs = get_last_updated();
foreach ($blogs AS $blog) {
switch_to_blog($blog["blog_id"]);
$lastposts = get_posts('numberposts=3');
foreach($lastposts as $post) :
setup_postdata($post);
// Starting HTNL output:
?>

<li>
<div>



<div>
<h2><?php the_title(); ?></h2>
</div>
</div>
</li>
<?php endforeach;
restore_current_blog();
}
?>


It is working but it gets the latest posts by blog ID and not the very last post from EACH BLOG BY DATE - which is what I need.

I also need to use it in the archive.php page with PAGINATION (showing only 10 and then start paginating) and it doesn't work because I don't know how to add pagination to my function.
so basically I need 2 things:
1) be able to get a query with the latest posts from all blogs BY DATE
2) be able to add pagination after 10 latest posts

** I would like a code which I can easily re-use and implement it in the search results page, archive page etc etc

Thanks

sd

Answers (4)

2015-07-28

Shoeb mirza answers:

There you go

1) Change your code $lastposts = get_posts('numberposts=3'); to
showposts=10&orderby=date&order=ASC'.'&paged='.$paged
You can check the settings of Wordpress.

Dashboard > Reading > Blog pages show at most


2) Add <?php echo custom_pagination(); ?>

function custom_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => __('<'),
'next_text' => __('>'),
'show_all' => True
) );
if( is_array( $pages ) ) {

$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '';
foreach ( $pages as $page ) {
echo $page;

}

}




hamergil comments:

Hi
This line:
showposts=10&orderby=date&order=ASC'.'&paged='.$paged
is not changing the fact that it takes all recent posts by blog ID first and then by date.
what I need is to ignore the blog ID's and just return all posts from all blogs order by dates
Thanks

2015-07-28

Andrea P answers:

I have not tested, but you can try this code


<?php

$blogs = get_last_updated();
// create an array where to store the posts
$recent_posts_global = array();
foreach ($blogs AS $blog) {
switch_to_blog($blog["blog_id"]);
$lastposts = get_posts('numberposts=3');
if ( $lastposts ){
// add the results to the main array
$recent_posts_global = array_merge($recent_posts_global, $lastposts);
}
restore_current_blog();
}
// now loop the main array and populate an array that we'll use to order it
$ordering_array = array();
foreach ( $recent_posts_global as $key => $post ){
setup_postdata($post);
// store the date and key in the ordering array
$ordering_array[$key] = get_the_time('U', $post->ID);
}
reset_postdata();

// now sort the main array using the data from the ordering array
array_multisort( $ordering_array, SORT_DESC, $recent_posts_global );

// now we can loop the ordered global array
foreach ( $recent_posts_global as $post ){
setup_postdata($post);
// Starting HTNL output:
?>
<li>
<div>
<div>

<h2><?php the_title(); ?></h2>

</div>

</div>

</li>
<?php
}
reset_postdata();

?>


hamergil comments:

Sorry it is not working.


Andrea P comments:

hello,

what exactly is happening? does it gives any error? if yes, which one?
does it break the layout/site?
does it print out anything or not?

2015-07-28

Bob answers:

Have you tried this plugin?
[[LINK href="https://wordpress.org/plugins/mu-helpers/"]]https://wordpress.org/plugins/mu-helpers/[[/LINK]]

Install it and use the function get_all_blog_posts( $num_per_blog = 3, $orderby = 'date', $sort = 'post_date_gmt' );

You can also try this function to get recent posts on network
source: http://www.smashingmagazine.com/2011/11/wordpress-multisite-practical-functions-methods/

/**
* List recent posts across a Multisite network
*
* @uses get_blog_list(), get_blog_permalink()
*
* @param int $size The number of results to retrieve
* @param int $expires Seconds until the transient cache expires
* @return object Contains the blog_id, post_id, post_date and post_title
*/
function wp_recent_across_network( $size = 10, $expires = 7200 ) {
if( !is_multisite() ) return false;

// Cache the results with the WordPress Transients API
// Get any existing copy of our transient data
if ( ( $recent_across_network = get_site_transient( 'recent_across_network' ) ) === false ) {

// No transient found, regenerate the data and save a new transient
// Prepare the SQL query with $wpdb
global $wpdb;

$base_prefix = $wpdb->get_blog_prefix(0);
$base_prefix = str_replace( '1_', '' , $base_prefix );

// Because the get_blog_list() function is currently flagged as deprecated
// due to the potential for high consumption of resources, we'll use
// $wpdb to roll out our own SQL query instead. Because the query can be
// memory-intensive, we'll store the results using the Transients API
if ( false === ( $site_list = get_site_transient( 'multisite_site_list' ) ) ) {
global $wpdb;
$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY id = %d, $id') );
set_site_transient( 'multisite_site_list', $site_list, $expires );
}

$limit = absint($size);

// Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
foreach ( $site_list as $site ) {
if( $site == $site_list[0] ) {
$posts_table = $base_prefix . "posts";
} else {
$posts_table = $base_prefix . $site->blog_id . "_posts";
}

$posts_table = esc_sql( $posts_table );
$blogs_table = esc_sql( $base_prefix . 'blogs' );

$query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table\n";
$query .= "\tWHERE $posts_table.post_type = 'post'\n";
$query .= "\tAND $posts_table.post_status = 'publish'\n";
$query .= "\tAND $blogs_table.blog_id = {$site->blog_id})\n";

if( $site !== end($site_list) )
$query .= "UNION\n";
else
$query .= "ORDER BY post_date DESC LIMIT 0, $limit";
}

// Sanitize and run the query
$query = $wpdb->prepare($query);
$recent_across_network = $wpdb->get_results( $query );

// Set the Transients cache to expire every two hours
set_site_transient( 'recent_across_network', $recent_across_network, 60*60*2 );
}

// Format the HTML output

$html = '<ul>';
foreach ( $recent_across_network as $post ) {
$html .= '<li><a>blog_id $post->ID ) . \'">' . $post->post_title . '</a></li>';
}
$html .= '</ul>';

return $html;
}
?>


Bob comments:

let me know if above function work. Then we can try to modify it for pagination related code.


hamergil comments:

Hi
Unfortunately the function above doesn't work..
it gives errors and even after I fix the errors it displays nothing in the site..


Bob comments:

May I know error please?

have added any filter which can effect global query?

2015-07-29

srikanto answers:

<?php



$blogs = get_last_updated();

// create an array where to store the posts

$recent_posts_global = array();

foreach ($blogs AS $blog) {

switch_to_blog($blog["blog_id"]);

$lastposts = get_posts('numberposts=3');

if ( $lastposts ){

// add the results to the main array

$recent_posts_global = array_merge($recent_posts_global, $lastposts);

}

restore_current_blog();

}

// now loop the main array and populate an array that we'll use to order it

$ordering_array = array();

foreach ( $recent_posts_global as $key => $post ){

setup_postdata($post);

// store the date and key in the ordering array

$ordering_array[$key] = get_the_time('U', $post->ID);

}

reset_postdata();



// now sort the main array using the data from the ordering array

array_multisort( $ordering_array, SORT_DESC, $recent_posts_global );



// now we can loop the ordered global array

foreach ( $recent_posts_global as $post ){

setup_postdata($post);

// Starting HTNL output:

?>

<li>

<div>

<div>



<h2><?php the_title(); ?></h2>



</div>



</div>



</li>

<?php

}

reset_postdata();



?>


hamergil comments:

Hi
Tried it and it's not working.. it gives error instead