1

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

  • 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 – leymannx Apr 2 at 21:02
  • @leymannx: ok, I edited the question with my current workaround. – gordie Apr 2 at 21:13
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.

  • Post Views Counter seems perfect ! Thanks. – gordie Apr 2 at 21:48
  • @gordie - Only thing to note about Post Views Counter, it currently doesn't let you set the view count manually (maybe necessary during development) via a meta box, but in post quick edit only instead. – leymannx Apr 3 at 4:53
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.