Title: Generic post sorting using post views
Last modified: August 22, 2016

---

# Generic post sorting using post views

 *  [migueleste](https://wordpress.org/support/users/migueleste/)
 * (@migueleste)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/)
 * Sharing my solution to generic post sorting using post views stored by wpp.
 * Essentially I’m storing the posts views for the interval I’m interested in a 
   custom field. Then use it to sort posts.
 * If you know about a more efficient way of doing it, please let me know.
 *     ```
       /* Storing 7 days postview count. add to functions.php or plugin*/
   
       function update_postviews($postid) {
        if (function_exists('wpp_get_views')) {
                $post_views7 = wpp_get_views( $postid, 'weekly' );
                if ( ! update_post_meta ($postid, 'postviews_7days', $post_views7) ) {
                       add_post_meta($postid, 'postviews_7days', $post_views7, true );
                       }
               }
       }
   
       add_action('wpp_update_views','update_postviews');
   
       /* end */
       ```
   
 *     ```
       /* use custom field to sort post queries */
   
       $args = array(
               'cat'      => $catid,
               'posts_per_page' => 10 ,
               'meta_key'              => 'postviews_7days',
               'orderby'               => 'meta_value_num',
               'order'                 => 'DESC',
       	);
   
       /* end */
       ```
   
 * In case you have a high traffic site, updates to the custom field may be much
   reduced without losing much accuracy. For example updating the count only on 
   around 10% of the post views:
 *     ```
       function update_postviews($postid) {
        if (function_exists('wpp_get_views') && (mt_rand(0,100)<10) ) {
                $post_views7 = wpp_get_views( $postid, 'weekly' );
                if ( ! update_post_meta ($postid, 'postviews_7days', $post_views7) ) {
                       add_post_meta($postid, 'postviews_7days', $post_views7, true );
                       }
               }
       }
       ```
   
 * Hector, thanks for the great plugin!
 * cheers,
    miguel
 * [https://wordpress.org/plugins/wordpress-popular-posts/](https://wordpress.org/plugins/wordpress-popular-posts/)

Viewing 8 replies - 16 through 23 (of 23 total)

[←](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/?output_format=md)
[1](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/?output_format=md)
2

 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831897)
 * Olá! Héctor,
 * Anida não funciono
 * 1 Adicionei no functions.php esse código abaixo.:
 *  /* Storing 7 days postview count. add to functions.php or plugin*/
    function
   update_postviews($postid) { if (function_exists(‘wpp_get_views’)) { $post_views7
   = wpp_get_views( $postid, ‘weekly’ ); if ( ! update_post_meta ($postid, ‘postviews_7days’,
   $post_views7) ) { add_post_meta($postid, ‘postviews_7days’, $post_views7, true);}}}
   add_action(‘wpp_post_update_views’,’update_postviews’);
 * 2 Adicionei esse código abaixo na index.php antes do <?php if (have_posts()) :?
   >
 * <?php
 * // The Query
    $args = array( ‘posts_per_page’ => 4, ‘post_type’ => ‘videos’, ‘
   meta_key’ => ‘post_views_count’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’,);
   $the_query = new WP_Query( $args );
 * // The Loop
    if ( $the_query->have_posts() ) { echo ‘
    - ‘;
       while ( $the_query->have_posts() ) { $the_query->the_post(); echo ‘
    - ‘ . get_the_title() . ‘
    - ‘;
       } echo ‘
 * ‘;
    } else { // no posts found }
 * /* Restore original Post Data */
    wp_reset_postdata();
 * ?>
 * E o seu plugin WordPress Popular Posts Versão 3.2.2 está ativado no site.
 * Sera que é o theme do meu WordPress que não aceita?
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831898)
 * That’s not right. The second code goes _outside_ the `if (have_posts()) :` condition(
   actually, you don’t need that).
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831899)
 * olá! Héctor, assim ainda não funcionou.
 * Coloquei assim
 *  <?php if (have_posts()) : ?>
 *  <?php
 * // The Query
    $args = array( ‘posts_per_page’ => 4, ‘post_type’ => ‘videos’, ‘
   meta_key’ => ‘post_views_count’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’,);
   $the_query = new WP_Query( $args );
 * // The Loop
    if ( $the_query->have_posts() ) { echo ‘
    - ‘;
       while ( $the_query->have_posts() ) { $the_query->the_post(); echo ‘
    - ‘ . get_the_title() . ‘
    - ‘;
       } echo ‘
 * ‘;
    } else { // no posts found }
 * /* Restore original Post Data */
    wp_reset_postdata();
 * ?>
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831900)
 * Again, that’s not right 😛
 * You need to move the code **outside** the `<?php if (have_posts()) : ?> <?php
   if endif; ?>` statement.
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831904)
 * Olá! Héctor,
 * Obrigado conseguir fazer funcionou…
 *  [radgh](https://wordpress.org/support/users/radgh/)
 * (@radgh)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831918)
 * The code provided by the OP no longer works. This was pointed out by Hector, 
   but it’s easy to miss!
 * I posted a separate thread here: [https://wordpress.org/support/topic/how-to-sort-a-custom-query-by-views-all-time-monthly-weekly-or-daily?replies=1](https://wordpress.org/support/topic/how-to-sort-a-custom-query-by-views-all-time-monthly-weekly-or-daily?replies=1)
 * I spent a good 20 minutes trying to debug the code before I noticed Hector’s 
   comment. It might be a good idea to sticky the working example to save some people
   the frustration in the future 🙂
 * I’m surprised this isn’t built-in to the plugin!
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831919)
 * Thanks for the update, [@radgh](https://wordpress.org/support/users/radgh/)!
 * The reason why this isn’t built-in into the plugin is simple: performance. While
   this code is really useful it also increases the number of database queries. 
   I prefer keeping the plugin as light as possible for everyone’s sake (including
   mine hehe). This isn’t set in stone and I might change my mind about it in the
   future, though 🙂
 *  [radgh](https://wordpress.org/support/users/radgh/)
 * (@radgh)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831920)
 * Yeah, I guess that was obvious. It could almost be a separate plugin by itself.
   But since it’s also not really useful unless you’re a developer, I guess there’s
   not much sense to build it versus building a function like the one above. Ah 
   well.
 * Thanks for the plugin as it is! I am definitely pleased with it, even lacking
   the feature.

Viewing 8 replies - 16 through 23 (of 23 total)

[←](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/?output_format=md)
[1](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/?output_format=md)
2

The topic ‘Generic post sorting using post views’ is closed to new replies.

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

## Tags

 * [sorting](https://wordpress.org/support/topic-tag/sorting/)
 * [views](https://wordpress.org/support/topic-tag/views/)

 * 23 replies
 * 5 participants
 * Last reply from: [radgh](https://wordpress.org/support/users/radgh/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/page/2/#post-5831920)
 * Status: not a support question