Closed norico closed 6 years ago
Hi @norico,
Currently each site has its own database table to store its own popular posts data. Adding such a feature could potentially lead to performance issues - specially if one or more sites under the network have a lot of posts / get a lot of visitors per day.
A possible solution would be to add a new database table to store a ranking of top articles from all the sites under the network, but this has its own caveats as well.
If more people show their interest in having this feature implemented, I'll consider adding it.
Can’t use switch_to_blog and store all data in new result array ? Thx
That could work, give it a shot. You could try something like this (pseudo code):
$popular = array();
$sites = function_to_get_all_sites(); // There's a WP function for this, just can't remember its name so please check the documentation
foreach( $sites as $site ) {
// Get site ID
// and then:
switch_to_blog( $site_id );
// Get popular posts from this site
$popular_posts = new WPP_Query( array('range' => 'last7days', 'limit' => 5) );
if ( $popular_posts ) {
$popular[ $site_id ] = $popular_posts;
}
} // End foreach
// Then, loop each site to get its popular posts, sort them by views and display them on your site
I try this now. Thx
@norico I have this exact question. Have you gotten this to work? Let me have a look at your code. Thx
I try to make a new function in plugin to make loop on all site, but can’t get any Wordpress function 😔 Need some help Thx
I'll create a function later. It shouldn't be complicated
If you're still struggling with this, share what you have so far and I might be able to help out.
$popular = array(); $sites = get_sites(); foreach( $sites as $site ) : $site_id = $site['blog_id']; switch_to_blog( $site_id ); // Get popular posts from this site $popular_posts = new WPP_Query( array('range' => 'last7days', 'limit' => 5) ); if ( $popular_posts ) { $popular[ $site_id ] = $popular_posts; } endforeach;
This function work in my theme but how to include inside widget
You can't. As I mentioned before, this is something WPP can't do natively which is why I suggested using the WPP_Query class instead.
You either insert it directly into your theme or if you prefer to "widgetize" this piece of code then you can build a custom widget just for this.
I try, i try.
I just want to get this : site_id[1] array([posts_id,post_count],[...]) site_id[2] array([posts_id,post_count],[...])
After array merge and sorting with post_count. It’s possible to write this in your class ?
Thx
Unfortunately, no.
😔 Uninstall
Can you insert a private function to get multi site top article. Top list from all sites. Thx