Possible bug: update_user_meta is updating two unique meta entries

On a CPT page I am checking to see if the user is logged in; if they are, I’m checking to see if they have a meta entry which is 'session-' + the current page’s stub. If they do, I increment the value by one.

I thought this was all working fine, but I’ve just noticed that it’s actually incrementing two separate user_meta fields. At the moment I have three CPT pages (let’s call them Burt, Ernie & Ralph); if I refresh the page for Burt, it increments my user_meta field (session-burt) by 1, which is great; however it also increments session-ralph by 1.

Read More

I don’t have the Ralph page open, and I’m echoing out everything I can think of to try and trace down the erroneous line of code, but I can’t see why it’s doing it. It is incredibly annoying.

Here’s my code. Hopefully I’m just blind to something really obvious and it’s not a WP bug.

$stub = $post->post_name;

$sessionKey = 'session-' . $stub;
$viewedKey = $stub . '-lastviewed';

$sessionViewsArray = get_user_meta($current_user->ID, $sessionKey);
$sessionViews = (int)$sessionViewsArray[0];

echo '<pre>';
print_r($sessionViewsArray);
echo '</pre>';

echo '<h1>I think there are ' . $sessionViews . ' views</h1>';

$lastViewedArray = get_user_meta($current_user->ID, $viewedKey);
$lastViewedTime = $lastViewedArray[0];

if ($lastViewedTime) {
    $lastViewed = date('l jS F Y, g:ia', $lastViewedTime);
} else {
    $lastViewed = 'First time - good luck!';
}

$newSessionViews = $sessionViews + 1;

update_user_meta($current_user->ID, $sessionKey, $newSessionViews);
update_user_meta($current_user->ID, $viewedKey, time());

echo '<h1>I think I just updated ' . $sessionKey . '</h1>';

///
//  Now let's update the page views
///

$pageViewsArray = get_post_meta($post->ID, 'totalviews');
$pageViews = (int)$pageViewsArray[0];

$pageViews += 1;

update_post_meta($post->ID, 'totalviews', $pageViews);

It’s worth noting that it only seems to be the one session-ralph entry that is affected; the ralph-lastviewed entry doesn’t get updated if I refresh the Burt page.

Any help would be greatly appreciated!

UPDATE:

As per the awesome answer from @dalbaeb below, my solution was to use the following hook:

remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

…which removed the <link rel='next'... and <link rel='prev'... tags from my header and immediately solved the issue.

Related posts

Leave a Reply

1 comment

  1. Are you by chance using Firefox to test your page? If so its prefetching function combined with WordPress rel='next' could be the culprit. Check your page source for those rel tags, and see if Firebug registers an extra GET request to the page specified in the href attribute of the rel tag.

    There is a Firefox extension Fasterfox, and it will prefetch everything. Disabling adjacent_posts_rel_link_wp_head will not help.
    You should exit early in your code in those cases:

    if ( isset ( $_SERVER["HTTP_X_MOZ"] ) and 'prefetch' === $_SERVER["HTTP_X_MOZ"] )
        return;