-1

I am using WP Post Views plugin to display the post number of views. To do so, I use:

<?php if(function_exists('the_views')) { the_views(); } ?>

The problem is: The plugin increments the number of a post views if the user just refreshes the post page. It will be good if the number of views increase only one time for a specific user.

I thank you for your time.Your usual help is always appreciated.

The link to the code source is :http://plugins.trac.wordpress.org/browser/wp-postviews/trunk/wp-postviews.php

Note:

I already managed, with the help of SE community, to exclude the views by post author using the following hack:

function remove_view_counter_wpse_102637() {
  global $post;
  $current_user = wp_get_current_user();

  if (
    is_single() 
    && !empty($current_user)
    && $post->post_author == $current_user->ID
  ) {
    remove_action('wp_head', 'process_postviews');
  }
}
add_action('wp_head', 'remove_view_counter_wpse_102637',1);

But I cannot manage to exclude the multiple counts by same user using the same logic.

2 Answers 2

1

Since the plugin author does not offer any actions or filters for us to hook onto, we'll have to settle for "listening" to changes to the views meta field instead.

function wpse_104324_prevent_multiple_views( $meta_id, $object_id, $meta_key, $meta_value ) {
    if ( $meta_key === 'views' ) {
        if ( ! empty( $_COOKIE[ USER_COOKIE . '_views' ] ) )
            $viewed = array_map( 'intval', explode( ',', $_COOKIE[ USER_COOKIE . '_views' ] ) );
        else
            $viewed = array();

        $viewed[] = $object_id;
        setcookie(
            USER_COOKIE . '_views',
            implode(
                ',', $viewed
            ),
            time() + 31536000,
            COOKIEPATH,
            COOKIE_DOMAIN,
            false,
            true
        );
    }
}

add_action( 'updated_post_meta', 'wpse_104324_prevent_multiple_views', 10, 4 );
add_action( 'added_post_meta',   'wpse_104324_prevent_multiple_views', 10, 4 );

What happens now, is that whenever views is modified, we add the current post ID to the current user's "view stack", then save it as a cookie.

With this in place, we can now check to see if a user has already viewed the post, and if so, prevent their view count from being saved again.

if ( ! empty( $_COOKIE[ USER_COOKIE . '_views' ] ) ) {
    $viewed = array_map( 'intval', explode( ',', $_COOKIE[ USER_COOKIE . '_views' ] ) );
    if ( in_array( $post->ID, $viewed ) )
        remove_action( 'wp_head', 'process_postviews' );
}

Put this last code snippet inside the remove_view_counter_wpse_102637 function.

3
  • I thank you very much. I read the codes you provided and I clearly see that the logic behind will surely resolve my problem. I will implement this and come back to accept. Commented Jun 26, 2013 at 16:25
  • this is working perfectly, I am really thanking you for the solution and for the cookies science I learned from it. Good luck in your projects. Commented Jun 26, 2013 at 17:00
  • How can I solve the "Cannot modify header information - headers already sent by " warning caused by your suggested code. Thank you Commented Oct 24, 2013 at 18:17
0

replace your function by following code :

function remove_view_counter_wpse_102637() {
if ( ! empty( $_COOKIE[ USER_COOKIE . '_views' ] ) ) {
    $viewed = array_map( 'intval', explode( ',', $_COOKIE[ USER_COOKIE . '_views' ] ) );
    if ( in_array( $post->ID, $viewed ) )
        remove_action( 'wp_head', 'process_postviews' );
}
global $post;
$current_user = wp_get_current_user();

if (
  is_single() 
  && !empty($current_user)
  && $post->post_author == $current_user->ID
) {
  remove_action('wp_head', 'process_postviews');
}
}
add_action('wp_head', 'remove_view_counter_wpse_102637',1); 
2
  • 1
    Welcome to the WordPress Development community! Answers tend to receive more attention when they use a little text to elaborate on what was wrong in the asker's solution, or how the answer's solution solves the issue described in the question.
    – bosco
    Commented Jun 12, 2021 at 15:36
  • Where does the USER_COOKIE_views cookie come from? Is that done by the plugin already? (I'd guess not, since TheDeadMedic's answer sets the cookie itself, but you've only copied over part of the code from that answer.)
    – Rup
    Commented Jun 12, 2021 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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