如果要在特定时间段内存储数据,可以使用Transients API. 但是,此API适用于少量数据,例如主题选项或类似数据。
您可以做的是,正常存储post视图,然后每天运行cron作业并清除视图。让我们看一个简单的例子。
// You update the view counts by using post meta
update_post_meta( $post_id, "view_count", $views + 1 );
// Now, let\'s run a task each day at 00:00 and clear these counts.
// If the cron is not scheduled, schedule it for 00:00
if ( ! wp_next_scheduled( \'clear-post-counts\' ) ) {
wp_schedule_event( strtotime( \'00:00:00\' ), \'daily\', \'clear-post-counts\' );
}
// Every time this action hook is triggered, the
// callback function is run to clear the meta data
add_action(\'clear-post-counts\', \'clear_post_counts\');
function clear_post_counts(){
// Get a list of all posts
$posts = get_posts();
// For each post, set the post views to 0
foreach( $posts as $post ) {
update_post_meta( $post->ID, "view_count", 0);
}
}
该代码只是一个从何处开始的示例。您可以自定义它以满足您的特定需要。