• Resolved Nurbis

    (@nurbis)


    Hi

    I made new page and set there query which should show most popular posts weekly.

    $query = new WP_Query( array( 'paged' => get_query_var('paged'), 'meta_key' => 'views_weekly', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );

    But it is clearly showing wrong posts (not the most popular). Ive got also widget at sidebar with popular posts :

    $args = array(	'limit' => 20, 'range' => 'weekly', 'stats_views' => 0, 'post_type' => 'post');
    wpp_get_mostpopular( $args );

    And this widget is showing correctly the most visited posts. And I cant find out why these 2 queries are showing me completely different posts.
    I set sample rate 30% for both the plugin settings and function saving views to meta fields, so the number of views at metafields is the same as on plugin settings page. So why does wordpress doesnt order those posts correctly ?

    Thank you

    • This topic was modified 7 years, 7 months ago by Nurbis.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi there!

    Please share the full code (including the function you’re using to store / update the meta keys) so I can help you out.

    Thread Starter Nurbis

    (@nurbis)

    Hi

    Functions.php :

    add_action( 'pre_get_posts', 'change_posts_on_homepage' );
    /* Storing views of different time periods as meta keys */
    add_action( 'wpp_post_update_views', 'custom_wpp_update_postviews' );
    function custom_wpp_update_postviews($postid) {
    	// Accuracy:
    	//   10  = 1 in 10 visits will update view count. (Recommended for high traffic sites.)
    	//   30 = 30% of visits. (Medium traffic websites)
    	//   100 = Every visit. Creates many db write operations every request.
    	$accuracy = 30;
    	if ( function_exists('wpp_get_views') && (mt_rand(0,100) < $accuracy) ) {
    		// Remove or comment out lines that you won't be using!!
    		update_post_meta( $postid, 'views_total',   wpp_get_views( $postid )            );
    		update_post_meta( $postid, 'views_daily',   wpp_get_views( $postid, 'daily' )   );
    		update_post_meta( $postid, 'views_weekly',  wpp_get_views( $postid, 'weekly' )  );
    		update_post_meta( $postid, 'views_monthly', wpp_get_views( $postid, 'monthly' ) );
    	}
    }

    Custom_page.php

    <?php $query = new WP_Query( array( 'paged' => get_query_var('paged'), 'meta_key' => 'views_weekly', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
    										if ($query->have_posts()) : ?>
    								   		<?php while ($query->have_posts()) : $query->the_post(); ?>
    	  
    										<!-- post -->
    	
    										<!-- // post -->
    										
    								   		<?php endwhile;?>

    sidebar.php

    <?php
    	$args = array(
    		'limit' => 20,
    		'range' => 'weekly',
    		'stats_views' => 0,
    		'post_type' => 'post'
    	);
    	wpp_get_mostpopular( $args );
    ?> 
    • This reply was modified 7 years, 7 months ago by Nurbis.
    • This reply was modified 7 years, 7 months ago by Nurbis.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey Nurbis!

    This works for me:

    // custom_page.php
    <?php
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 10,
        'meta_key' => 'views_weekly',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    );
    
    $posts = new WP_Query( $args );
    
    if ( $posts->have_posts() ) :
    
        echo '<ul>';
    
        while( $posts->have_posts() ) : $posts->the_post();
            echo '<li>' . get_the_title() . '</li>';
        endwhile;
    
        echo '</ul>';
    
    endif;
    
    wp_reset_postdata();
    ?>

    Here’s the output: https://prnt.sc/gdmii3

    Your problem lies here:

    I set sample rate 30% for both the plugin settings and function saving views to meta fields, so the number of views at metafields is the same as on plugin settings page.

    Both listing are updating its views count at a random time (your custom_wpp_update_postviews function doesn’t necessarily updates the views count at the same time as the plugin does) which results in both listings not displaying the exact same posts at the exact same order.

    To fix this, you only need to keep WPP’s Data Sampling enabled. Why? Because the wpp_post_update_views action hook will be triggered only when WPP updates its views count (that is when WPP’s Data Sampling condition is met.) So, your custom_wpp_update_postviews function should be like this instead:

    /* Storing views of different time periods as meta keys */
    function custom_wpp_update_postviews( $postid ) {
        if ( function_exists('wpp_get_views') ) {
            update_post_meta( $postid, 'views_total',   wpp_get_views( $postid )            );
            update_post_meta( $postid, 'views_daily',   wpp_get_views( $postid, 'daily' )   );
            update_post_meta( $postid, 'views_weekly',  wpp_get_views( $postid, 'weekly' )  );
            update_post_meta( $postid, 'views_monthly', wpp_get_views( $postid, 'monthly' ) );
        }
    }
    add_action( 'wpp_post_update_views', 'custom_wpp_update_postviews' );
    • This reply was modified 7 years, 7 months ago by Hector Cabrera. Reason: Added screen capture
    • This reply was modified 7 years, 7 months ago by Hector Cabrera. Reason: Added screen capture
    • This reply was modified 7 years, 7 months ago by Hector Cabrera. Reason: Adds clarification
    • This reply was modified 7 years, 7 months ago by Hector Cabrera. Reason: Adds further clarification
    Thread Starter Nurbis

    (@nurbis)

    Hi
    Thanks for the answer. I changed the function yesterday but it seems to be still not working (when I’m comparing plugin page stats for 24hrs and custom page with daily_views).
    But I looked to the phpmyadmin to wp_postmeta and the column views_daily has correct data. So the post showing on my custom page as “the most viewed” has significantly less views (in the column) then the truly most viewed.
    So might this be some other problem with wordpress ? Like problem with caching or something ? Or am I still doing something wrong with the plugin ?

    EDIT: I tried to turn off database caching (from W3TC) but it didn’t help.

    • This reply was modified 7 years, 6 months ago by Nurbis.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Try disabling W3TC completely and check again (and clear W3TC’s cache when you do to make sure nothing is being cached by it.)

    Thread Starter Nurbis

    (@nurbis)

    Yeah, nothing changes after deactivating.

    EDIT: I’m also using plugin “WP_postviews” with metafield “views” and when I tried to sort the posts by field “views” it worked correctly. Strange.

    • This reply was modified 7 years, 6 months ago by Nurbis.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey there!

    Well, as I mentioned above your code worked as expected when I tested it on my development site so it must be something else on your side.

    Thread Starter Nurbis

    (@nurbis)

    Hi

    I just noticed at phpmyadmin that your number of views using format 1,000 instead of 1000. Couldn’t that be a problem ?
    See this screenshot :
    https://pixhost.org/show/375/50539942_sni-mek-obrazovky-2017-09-01-v-16-15-09.png

    The field “views” is from wp_postviews plugin and sorting via this field works properly.

    EDIT: Yes, when I changed the format at todays most viewed post from “2,910” to “2910” the query is showing it correctly as number one. So all the posts the query showed me was posts with views under 1000 …
    So can you please update the function so the numbers will be stored properly ? Thank you.

    • This reply was modified 7 years, 6 months ago by Nurbis.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Ah, you’re right! No need to update anything though, the wpp_get_views() template tag already does what you need:

    /* Storing views of different time periods as meta keys */
    function custom_wpp_update_postviews( $postid ) {
    
        if ( function_exists('wpp_get_views') ) {
            update_post_meta( $postid, 'views_total', wpp_get_views( $postid, 'all', false ) );
            update_post_meta( $postid, 'views_daily', wpp_get_views( $postid, 'daily', false ) );
            update_post_meta( $postid, 'views_weekly', wpp_get_views( $postid, 'weekly', false ) );
            update_post_meta( $postid, 'views_monthly', wpp_get_views( $postid, 'monthly', false ) );
        }
    
    }
    add_action( 'wpp_post_update_views', 'custom_wpp_update_postviews' );

    Please make sure to check the documentation next time, it’ll save you time.

    • This reply was modified 7 years, 6 months ago by Hector Cabrera. Reason: Wrapped code in backticks
    Thread Starter Nurbis

    (@nurbis)

    Cool, thank you ?? Will do next time.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Query shows wrong posts’ is closed to new replies.