• 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://www.ads-software.com/plugins/wordpress-popular-posts/

Viewing 15 replies - 1 through 15 (of 23 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi Miguel,

    That’s a pretty interesting approach! I’m sticking this topic so others can check it out.

    Thanks for sharing!

    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

    (@danielambrosio)

    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://www.ads-software.com/plugins/wordpress-popular-posts/

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hi Daniel!

    I’d suggest using WP_Query instead. 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

    (@danielambrosio)

    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

    (@hcabrera)

    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

    (@danielambrosio)

    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

    (@hcabrera)

    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 to your theme’s functions.php file?

    By the way, everyone: the wpp_update_views hook 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

    (@danielambrosio)

    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

    (@hcabrera)

    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

    (@danielambrosio)

    Olá,

    Eu mudou aqui functions.php mudar wpp_update_views para wpp_post_update_views ?

    Veja.site

    (@danielambrosio)

    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

    (@hcabrera)

    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

    (@danielambrosio)

    Ok obrigado esse post aqui na guardado no meu favorito é só posta aqui amigo obrigado pela aten??o mesmo.

    Plugin Author Hector Cabrera

    (@hcabrera)

    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:
      /* 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. Add this code 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)
  • The topic ‘Generic post sorting using post views’ is closed to new replies.