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