无法确定每个页面视图的录制时间。它只是记录页面视图,而不是记录发生的时间。
为了解决这个问题,您可以为每个视图保留一个post\\u元条目,但这可能会在流行网站上创建很多条目。或者,您可以保持每日计数;删除任何超过2天的内容(如果您只对48小时感兴趣),并保持运行总数。
以下是每日计数法的建议解决方案。。。
要添加每日计数。。。
$post_id = get_the_ID();
$post_view_count = get_post_meta( $post_id, \'view_count\', true );
$post_daily_view_count = get_post_meta( $post_id, \'view_daily_counts\', true );
$post_recent_count = get_post_meta( $post_id, \'view_recent_count\', true );
if ( $post_view_count == \'\' ) {
update_post_meta( $post_id, \'view_count\', 1 );
} else {
$post_view_count = intval( $post_view_count );
++$post_view_count;
update_post_meta( $post_id, \'view_count\', $post_view_count );
}
$today = new DateTime;
$days_to_consider = 2; // How many days to consider when looking for recent views?
$delete_befre_date = new DateTime( "-{$days_to_consider} days" );
// Create the meta entry if doesn\'t already exist
if ( $post_daily_view_count == \'\' ) {
$post_daily_view_count = array( $today->format( \'Y-m-d\' ) => 1 );
// Otherwise, update the existing one
} else { // if ( $post_daily_view_count == \'\' )
// Update the entry for today
$post_daily_view_count[$today->format( \'Y-m-d\' )] = intval( $post_daily_view_count[$today->format( \'Y-m-d\' )] );
++$post_daily_view_count[$today->format( \'Y-m-d\' )];
} // if ( $post_daily_view_count == \'\' )
// Calculate the recent total, removing entries older than X days from the recent counts
$recent_count = 0;
foreach ( array_keys( $post_daily_view_count ) as $date ) {
if ( $date < $delete_befre_date->format( \'Y-m-d\' ) ) {
unset( $post_daily_view_count[$date] );
} else {
$recent_count = $recent_count + $post_daily_view_count[$date];
}
}
// And save the update meta
update_post_meta( $post_id, \'view_daily_counts\', $post_daily_view_count );
update_post_meta( $post_id, \'view_recent_count\', $recent_count );
然后查询帖子。。。
$args_popular = array(
\'posts_per_page\' => \'5\',
\'order\' => \'DESC\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'view_recent_count\',
\'cache_results\' => true,
);
要更改保留的天数,请更改
$days_to_consider
在第一段代码中。