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.