简体   繁体   中英

How to get the number of visits to an article in 24 hours?

I use this function to save article views

function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
        
        
    }
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

is it possible to get views count in 24 hours for example ?

and get most post viewed in 24 hours ?

Try to use a serialized array:

function wpb_set_post_views( $postID ) {
    $count_key = 'wpb_post_views_count';
    $date = date('Ymd');
    $views = get_post_meta($postID, $count_key, true);
    if( !empty($views) AND is_array($views) AND !empty($views[$date]) ){
        $views[$date] = $views[$date] + 1;
    } else {
        $views = array($date => 1);
    }
    update_post_meta($postID, $count_key, $views );
    return $views[$date];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM