admin管理员组

文章数量:1434924

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 );
        }
    }
}

本文标签: transienthow to build (custom) stats for post viewsper month