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 15 replies - 1 through 15 (of 23 total)

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

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831619)
 * Hi Miguel,
 * That’s a pretty interesting approach! I’m sticking this topic so others can check
   it out.
 * Thanks for sharing!
 *  [aaroneight](https://wordpress.org/support/users/aaroneight/)
 * (@aaroneight)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831870)
 * This is awesome. Thanks for sharing, Miguel.
 * Used this for custom query for an RSS feed to show feed the latest 10 popular
   WPP posts.
 * Thanks for sharing!
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831880)
 * **Hello! Héctor,**
 * How do I do that my posts index of home page is 10th most popular posts for visits.
   
   <?php query_posts(array(‘post_type’ => ‘videos’, ‘meta_key’ => ‘post_views_count’,‘
   orderby’ => ‘meta_value’,’order’ => ‘DESC’,’showposts’ => 4)); ?> <?php while(
   have_posts()) : the_post(); ?>
 * Do this by using your plugin [https://wordpress.org/plugins/wordpress-popular-posts/](https://wordpress.org/plugins/wordpress-popular-posts/)
 *  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/#post-5831881)
 * Hi Daniel!
 * [I’d suggest using WP_Query instead](http://wordpress.stackexchange.com/a/1755).
   Here’s an (untested) example:
 *     ```
       <?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 '<ul>';
       	while ( $the_query->have_posts() ) {
       		$the_query->the_post();
       		echo '<li>' . get_the_title() . '</li>';
       	}
       	echo '</ul>';
       } else {
       	// no posts found
       }
   
       /* Restore original Post Data */
       wp_reset_postdata();
   
       ?>
       ```
   
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831882)
 * **Hi Héctor,**
 * There does not work you do not know any plugin that will deixano the post of 
   home index on top when going accessing or commenting?
 *  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/#post-5831883)
 * Sorry, didn’t understand any of that. Do you speak spanish, by any chance? If
   so, please post in spanish and I’ll do the translation.
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831884)
 * **Hi Héctor,**
 * Eu falou em português sou do Brasil é que eu traduzir minha descrição.
 * Ok vou manda em português e você traduzir.
 * Esse scripts não funcionou na home do meu website eu queria que os 10 posts populares
   da home fica no topo dos mais acessado ou mias comentado seria melhor. Você não
   sabe algum plugin que tem essa funcionalidade isso não da pra fazer no seu plugin?
 * Muito obrigado pela atenção **Héctor**.
 *  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/#post-5831885)
 * Ah, right! I forgot! You told me that before.
 * For the script to work you need to do what the OP described on the first post
   of this topic. Did you add the [`wpp_update_views` hook](https://github.com/cabrerahector/wordpress-popular-posts/wiki/4.-Actions#wpp_post_update_views-formerly-wpp_update_views)
   to your theme’s functions.php file?
 * By the way, everyone: the [`wpp_update_views` hook](https://github.com/cabrerahector/wordpress-popular-posts/wiki/4.-Actions#wpp_post_update_views-formerly-wpp_update_views)
   will be renamed as `wpp_post_update_views` on the next version of the plugin.
   Make sure to update your code once 3.2.2 is out!
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831886)
 * **Olá Héctor,**
 * Eu instalei a versão do plugin 3.2.2 wordpress-popular-posts-3.2.2-beta-3
 * E instalei o que eu tenho que fazer agora ?
 * Adiciona aquele código que você me mandou no comentário la em cima ?
 * O que eu tenho que colocar no arquivo functions.php ?
 * Obrigado pela atenção **Héctor**.
 *  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/#post-5831887)
 * Ah, if you’re using the beta version you need to change `wpp_update_views` to`
   wpp_post_update_views`. And yes, that code goes into functions.php.
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831888)
 * Olá,
 * Eu mudou aqui functions.php mudar wpp_update_views para wpp_post_update_views?
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831889)
 * Olá,
 * Mudei no functions.php para wpp_post_update_views 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’);
 * /* end */
 * Não está funcionando deixa amigo vou arruma uma outra maneira no meu site de 
   fazer isso obrigado pela atenção.
 *  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/#post-5831890)
 * I’m a bit busy right now so I can’t test it at the moment. I’ll get back to this
   during the weekend and see what’s going on.
 *  [Veja.site](https://wordpress.org/support/users/danielambrosio/)
 * (@danielambrosio)
 * [11 years ago](https://wordpress.org/support/topic/generic-post-sorting-using-post-views/#post-5831891)
 * Ok obrigado esse post aqui na guardado no meu favorito é só posta aqui amigo 
   obrigado pela atenção mesmo.
 *  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/#post-5831896)
 * Hi Daniel!
 * Just did some testing and the code works for me. Here’s what I did:
    1. Add this code to your theme’s functions.php:
    2.     ```
           /* 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');
           ```
       
    3. Add [this code](https://wordpress.org/support/topic/generic-post-sorting-using-post-views?replies=15#post-6826873)
       to your home template (adjust where necessary).
 * That’s it!
 * Note that for this to work your posts need to register the _postviews\_7days_
   post meta first. This will happen automatically once someone visits your posts/
   pages (that’s what the first code is for), so you’ll need to wait for a while
   to start seeing some results.

Viewing 15 replies - 1 through 15 (of 23 total)

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

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