cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
279 stars 83 forks source link

Multi site top #172

Closed norico closed 6 years ago

norico commented 6 years ago

Can you insert a private function to get multi site top article. Top list from all sites. Thx

cabrerahector commented 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.

norico commented 6 years ago

Can’t use switch_to_blog and store all data in new result array ? Thx

cabrerahector commented 6 years ago

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
norico commented 6 years ago

I try this now. Thx

lubandi commented 6 years ago

@norico I have this exact question. Have you gotten this to work? Let me have a look at your code. Thx

norico commented 6 years ago

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

lubandi commented 6 years ago

I'll create a function later. It shouldn't be complicated

cabrerahector commented 6 years ago

If you're still struggling with this, share what you have so far and I might be able to help out.

norico commented 6 years ago

$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

cabrerahector commented 6 years ago

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.

norico commented 6 years ago

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

cabrerahector commented 6 years ago

Unfortunately, no.

norico commented 6 years ago

😔 Uninstall