I have created a post view counter plugin that increments a count each time a post is viewed/refreshed.

The count is working fine however if editing a live post the autosave functionality appears to grab the view count when the autosave is generated so when i update the post the view count reduces as the view count from when the autosave is created is used rather than the current value.

For example view count = 100, i open post to edit, autosave is generated, meanwhile 50 people view the post, i click update on the post, the view count drops from 150 back to 100

My plugin code is:

function update_post_views_counter()
        if(get_post_type() == "post")
            $id = get_the_ID();

            $views = get_post_meta($id, "total_views", true);

            if($views == "")
                $views = 1;
                add_post_meta($id, "total_views", $views);
                update_post_meta($id, "total_views", $views);

            maintain_top_posts($id, $views);

add_action("wp_head", "update_post_views_counter");

Is there a way to exclude updating the total_views post meta value when updating a post, when an autosave has been generated or not?

This is different to the common meta values being lost as the data is being posted just not the most recent data

  • I don't think this is a duplicate as with that post the problem is that the post meta data is being blanked because nothing is being posted. The data here is not being blanked but rather reverted to a previous state which means that a post value must be being sent to the save script unlike in that post – Leo May 6 '15 at 14:28
  • Sorry about that, it does look to be its own question and I've removed my comment. I'm not entirely sure why it's happening but it's certainly interesting and different. I think the problem is that the autosave has its own copy of metadata made when the autosave is generated. Updating the post is causing the autosaved post meta to overwrite the 'fresher' post meta. – kittsville May 6 '15 at 15:06

I have managed to find the reason behind this and create a solution suitable for my needs.

Under screen options on the post edit page is an option called "Custom Fields". This lists all of the custom meta values that have been saved against the post and allows you to create new ones and edit existing.

When opening the post to edit, the view count at that moment is added to the input field in the Custom Fields section. When i save several minutes later, even though the view count has increased the posted value is still that which was added when opening the edit window.

To fix this i added the following code to my themes functions file to remove the Custom Fields section from the edit window.

function remove_post_custom_fields() {
    remove_meta_box( 'postcustom' , 'post' , 'normal' ); 
add_action( 'admin_menu' , 'remove_post_custom_fields' );
| improve this answer | |

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