transient - how to build (custom) stats for post views, per month

Does anyone know how I could code something to get the most viewed posts per month (without Jetpack), ideally without ha

Does anyone know how I could code something to get the most viewed posts per month (without Jetpack), ideally without having to create a new DB table ?

Maybe there is a smart way to achieve this only using post metas and/or transients.

For the moment, i'm storing an array of stat entries as a post meta, containing a timestamp. Each time the meta is updated, I delete the timestamps < 1 month. It works, but i'm not sure that it is a good solution.

Any ideas ?

Thanks

Does anyone know how I could code something to get the most viewed posts per month (without Jetpack), ideally without having to create a new DB table ?

Maybe there is a smart way to achieve this only using post metas and/or transients.

For the moment, i'm storing an array of stat entries as a post meta, containing a timestamp. Each time the meta is updated, I delete the timestamps < 1 month. It works, but i'm not sure that it is a good solution.

Any ideas ?

Thanks

Share Improve this question edited Apr 2, 2019 at 21:49 gordie asked Apr 2, 2019 at 11:45 gordiegordie 4925 silver badges19 bronze badges 2
  • Hey gordie, a little bit broad your request. Do you have already started to set up anything yourself? If yes, please share your findings. If no, what answer exactly are you looking for? Please update your question for clarification. Thank you – norman.lol Commented Apr 2, 2019 at 21:02
  • @leymannx: ok, I edited the question with my current workaround. – gordie Commented Apr 2, 2019 at 21:13
Add a comment  | 

2 Answers 2

Reset to default 1

I think I'd build that upon and already existing plugin that does the counting for me. I have made quite good experience with Post Views Counter as it also lets you use it in WP_Query. But WP-PostViews looks promising as well. Choose one.

Next I'd query posts by view count from a WordPress cron event that runs monthly. And I'd simply save/add the results in an Options API option as array like $data[INTEGER_YEAR][INTEGER_MONTH][INTEGER_POST_ID][INTEGER_VIEW_COUNT]. And pull that off whenever I need it for displaying the high score in a widget or where ever.

You can track posts views with these functions. Create a new file and call it into your functions.php

after you collect enough data, you can create a meta box for Dashboard and you can use a chart module to view post views monthly/yearly etc.

add_action( 'wp_head', 'get_post_id' );
function get_post_id() {
    $postID = get_the_ID();
    return $postID;
}

function getPostViews() {
    $postID    = get_post_id();
    $count_key = 'views';
    $count     = get_post_meta( $postID, $count_key, true );
    if ( $count == '' ) {
        delete_post_meta( $postID, $count_key );
        add_post_meta( $postID, $count_key, '0' );
        return __( '0', 'sagive' );
    }
    return number_format( $count, '0', ',', '.' ) . __( '', 'sagive' );
}


remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
add_action( 'wp_head', 'setPostViews' );
function setPostViews() {
    if ( ! is_home() && ! is_robots() && ! is_user_logged_in() ) {
        $postID    = get_post_id();
        $count_key = 'views';
        $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 );
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633451a4637246.html

相关推荐

  • transient - how to build (custom) stats for post views, per month

    Does anyone know how I could code something to get the most viewed posts per month (without Jetpack), ideally without ha

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信