How can I order posts by views daily, weekly or monthly?
In your child theme's functions.php add:
function ghostpool_wpp_postviews( $post_id ) { // 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 = 50; if ( function_exists( 'wpp_get_views' ) && ( mt_rand( 0, 100 ) < $accuracy ) ) { update_post_meta( $post_id, 'views_total', wpp_get_views( $post_id ) ); //update_post_meta( $post_id, 'views_daily', wpp_get_views( $post_id, 'daily' ) ); //update_post_meta( $post_id, 'views_weekly', wpp_get_views( $post_id, 'weekly' ) ); //update_post_meta( $post_id, 'views_monthly', wpp_get_views( $post_id, 'monthly' ) ); } } function ghostpool_views_meta_key_func() { return 'views_weekly' } add_filter( 'ghostpool_views_meta_key' ,'ghostpool_views_meta_key_func' );
Uncomment the post meta field you want to use. To store the weekly views of posts use:
function ghostpool_wpp_postviews( $post_id ) { // 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 = 50; if ( function_exists( 'wpp_get_views' ) && ( mt_rand( 0, 100 ) < $accuracy ) ) { update_post_meta( $post_id, 'views_total', wpp_get_views( $post_id ) ); //update_post_meta( $post_id, 'views_daily', wpp_get_views( $post_id, 'daily' ) ); update_post_meta( $post_id, 'views_weekly', wpp_get_views( $post_id, 'weekly' ) ); //update_post_meta( $post_id, 'views_monthly', wpp_get_views( $post_id, 'monthly' ) ); } } function ghostpool_views_meta_key_func() { return 'views_weekly' // Replace with the field name you want to order by in filters } add_filter( 'ghostpool_views_meta_key' ,'ghostpool_views_meta_key_func' );
To update the new views data for each post find:
$accuracy = 50;
Replace with:
$accuracy = 100;
Now when each post is viewed by you or your visitors on the frontend it will update the views data.
You only need to do this step once. When you are done change the $accuracy value again.