• Resolved Bonesnap

    (@bonesnap)


    Hello all!

    I have run into a problem and I cannot figure out why it’s happening.

    A client wants posts to have a view counter on the post’s page. I’ve added a meta key to accommodate this. When the post is viewed, I use get_post_meta to retrieve the view counter, increase its value by one, and then update it.

    This works no problem; however, the NEXT post’s meta value is also updated by one. It always updates the current post and the next post. By next, I mean the post with the next greater ID.

    This is my code:

    the_post();
    get_template_part('content', 'page');
    if($post->ID)
    {
    	$views = get_post_meta($post->ID, 'gp_file_views', true);
    	if(!$views)
    		$views = 1;//First time the post has been viewed
    	else
    		$views++;//Otherwise increase its views by 1
    	update_post_meta($post->ID, 'gp_file_views', $views);
    }

    Things I’ve tried:

    – I tried removing get_template_part() altogether (and replaced simply with the_content()), but it still happens.

    – I’ve echoed out the $post->ID before retrieving the value and before updating it. It’s the same both times (as it should be).

    – I’ve tried assigning $post->ID to a variable and passing that instead but it makes no difference.

    – I’ve tried Googling for a solution but haven’t found anything.

    I’m using WordPress 3.2. If anyone has any insight into why this is happening I’d be greatly appreciated.

    Thank you for your time.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try moving the code inside the loop in content-page.php and content-single.php.

    Or try in header.php:
    Untested:

    <?php
    if( is_singular() ) {
      global $post;
      $views = get_post_meta($post->ID, 'gp_file_views', true) ? get_post_meta($post->ID, 'gp_file_views', true)++ : 1;
      update_post_meta($post->ID, 'gp_file_views', $views);
    }
    ?>

    HTH

    David

    Thread Starter Bonesnap

    (@bonesnap)

    I tried moving the code into both content-page.php and the header (not at the same time, of course) and it still happens. This is really stumping me. I’ve used get_post_meta and update_post_meta many times in the past without issue.

    Anyone have any other suggestions? Thanks!

    Hi,
    I have tested this with a Twenty Eleven child theme and here is what is happening:
    Choose a post in the list and View Counter + 1
    Previous post navigation and View Counter + 1
    Next post Navigation and the View Counter + 2

    So the double view counter is when you press “Next” it must resend the page or post.

    Looks like it is a core function issue, maybe reduce the counter in the function for next post, this will depend on the theme, and if it is a bug that is updated later in the core, search Trac?

    HTH

    David

    Thread Starter Bonesnap

    (@bonesnap)

    It’s not increasing the view count by 2. It’s increasing it by 1 but also increasing the view count of the following post. I’m not viewing the posts via navigation.

    If I select a post with post ID 5, and the next post has ID 11, they both increase by one. But if I have a post with ID 13, it’s not affected unless I select post 11, then 13 is increased as well.

    Also, it occurs even if I copy and paste the link directly into the address bar. No matter what I do, it increases BOTH posts.

    I even tried giving the post a unique meta key by appending the post’s ID to the meta key, but it still does it.

    Thread Starter Bonesnap

    (@bonesnap)

    Okay, so I’ve dug a little deeper into this problem and here’s what I’ve found:

    It has nothing to do with my code, but instead Firefox is running the page twice.

    I found a solution here.

    Thanks for your time and help!

    No Problem, I was testing with FireFox, never thought to try another browser.

    For anyone else that might read this topic, we were on the right track with the next post, it was being pre-loaded by FireFox.

    From the linked topic:

    //remove auto loading rel=next post link in header
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

    David

    I am working with custom post types a custom meta values for managing a banner rotator area in my theme.

    I insert each banner as a post with “custom post type” and then when a banner loads I’m tryng to “update_post_meta” for subtracting in the views limit meta wich I call “_printsleft”

    The thing is that when I was using “update_post_meta” in FIREFOX two of my banners was being updated… I noted that in some cases the same banner was uptaded twice so the “_printsleft” was being substracted two times.

    So in my case the update_post_meta isn’t happening in the next post unless in any random banner including the same banner.

    I tried the remove action adjacent_posts_rel_link_wp_head but it doesn’t fix the problem in firefox…

    in chrome my banners are sustracting its custom meta “_printsleft” correctly.

    please help!!

    Here is a possible solution suggested by my friend webdsgn.me.

    I will try to update_post_meta “_printsleft” only via ajax. The non-javascript user will be loosing my ads but I really need to update_post_meta right firefox!!

    ok, I’ll came back with news later

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘update_post_meta updates 2 posts’ is closed to new replies.