Title: Top 10   Polylang solution
Last modified: August 22, 2016

---

# Top 10 Polylang solution

 *  [Kestutis](https://wordpress.org/support/users/nelygus/)
 * (@nelygus)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/top-10-polylang-solution/)
 * So you are trying to use Top 10 plugin with Polylang?
    Simple solution:
 * to you function.php file add:
 *     ```
       /**
        * Top 10 most popular posts on wordpress plugin doesn't have Polylang plugin support,
        * so we have to filter posts on our own side before printing them out
        * @param $posts - current top 10 plugins posts
        * @return array - filtered array per language
        */
       function tptn_filter_posts_per_language($posts){
           global $polylang;
           $_posts = [];
           $limitPostsPerLanguage = 4; // show only 4 posts on home
           $currentLanguage = pll_current_language('slug');
   
           foreach($posts as $post) {
               $lang = $polylang->model->get_post_language($post['ID'])->slug;
               if ($currentLanguage == $lang) {
                   $_posts[] = $post;
               }
               if (count($_posts) >= $limitPostsPerLanguage) {
                   break;
               }
           }
           return $_posts;
       }
       add_filter('tptn_pop_posts_array','tptn_filter_posts_per_language');
       ```
   
 * That will filter all top10 posts to just show those which are in current language.
 * Then on place you want to print them:
 *     ```
       <?php if (function_exists('tptn_pop_posts')): ?>
                       <?php
                       // what? limit 100 when we are showing 4??
                       // oh yes, indeed. it's because we are filtering all posts by languages later.
                       // so there is possibility that with limit 10 there will be 9posts from same language.
                       // setting it to 100 we are assuming that there will be at least 4 posts from different language
                       $_top10posts = tptn_pop_posts(['posts_only' => true, 'limit' => 100]);
                       foreach($_top10posts as $_post):
                       ?>
                       <a href="<?php echo get_permalink($_post['ID']) ?>" title="<?php echo get_the_title($_post['ID']) ?>"><?php echo substr(get_the_title($_post['ID']), 0, 29) ?></a>
                       <?php
                       endforeach;
                       ?>
       <?php endif; ?>
       ```
   
 * I hope it will help someone.
 * [https://wordpress.org/plugins/top-10/](https://wordpress.org/plugins/top-10/)

Viewing 13 replies - 1 through 13 (of 13 total)

 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931189)
 * Thank you for the code!
 *  [Dennis Asp](https://wordpress.org/support/users/zyperdk/)
 * (@zyperdk)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931533)
 * I suppose it works, but I just get my post listed with no line breaks, no thumbnails
   or anything…
 * Simply goes like this:
    Post number 1 (tit title of post 2
 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931534)
 * That’s how the second piece of code that nelygus uses. You shouldn’t need the
   second piece of code.
 * See another example:
    [https://gist.github.com/ajaydsouza/c8defd4b46e53240e376](https://gist.github.com/ajaydsouza/c8defd4b46e53240e376)
 *  [Ahmis](https://wordpress.org/support/users/ahmis/)
 * (@ahmis)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931550)
 * This code seems to display the most popular posts of all time. What if I wanted
   to show the daily popular posts using this plugin and Polylang?
 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931551)
 *     ```
       $_top10posts = tptn_pop_posts(['posts_only' => true, 'limit' => 100, 'daily' => TRUE]);
       ```
   
 *  [Ahmis](https://wordpress.org/support/users/ahmis/)
 * (@ahmis)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931552)
 * Thanks a ton WebberZone! 🙂
 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931553)
 * You’re welcome
 *  [nathk](https://wordpress.org/support/users/nathk/)
 * (@nathk)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931560)
 * Thanks so much everyone!
 * It works with the text but what should I do if I only want to see the thumbnails
   instead?
 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931562)
 *     ```
       <a href="<?php echo get_permalink($_post['ID']) ?>" title="<?php echo get_the_title($_post['ID']) ?>"><?php the_post_thumbnail(); ?></a>
       ```
   
 * But, note that this relies that the post has a thumbnail..
 *  [bueniu](https://wordpress.org/support/users/bueniu/)
 * (@bueniu)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931576)
 * Hi, i tried all your propositions but for some reason the plugin does not seem
   to work. When i use the widget i get only the title . The i tried to use the 
   code above this is what i got:
 * `true, 'limit' => 100]); foreach($_top10posts as $_post): ?>`
 * [https://drive.google.com/file/d/0BxX7lV_tpmxbTkwzNmpvaW9UakU/view?usp=sharing](https://drive.google.com/file/d/0BxX7lV_tpmxbTkwzNmpvaW9UakU/view?usp=sharing)
 * I’m not a coder, so this does not tell me anything.
 * I would be grateful for help.
 * BTW. My goal is to make two separate widgets that show the most popular posts
   in current language (two, because i need the title to be different)
 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931577)
 * Are you in a position to test the Github version?
 * [https://github.com/WebberZone/top-10](https://github.com/WebberZone/top-10)
 *  [bueniu](https://wordpress.org/support/users/bueniu/)
 * (@bueniu)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931578)
 * Will try and let you know – thank you for the quick response!!! You are awesome!
 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931579)
 * Thank you. I’m curious to know. I’ve implemented basic support for PolyLang in
   the dev version in the repo. But, it’s always good to get some feedback on if
   it works well enough

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Top 10 Polylang solution’ is closed to new replies.

 * ![](https://ps.w.org/top-10/assets/icon-256x256.png?rev=2986432)
 * [WebberZone Top 10 — Popular Posts](https://wordpress.org/plugins/top-10/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/top-10/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/top-10/)
 * [Active Topics](https://wordpress.org/support/plugin/top-10/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/top-10/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/top-10/reviews/)

 * 13 replies
 * 7 participants
 * Last reply from: [WebberZone](https://wordpress.org/support/users/webberzone/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/top-10-polylang-solution/#post-5931579)
 * Status: not a support question