I am using a specific plugin to count my post views.
It store in a postmeta custom field named “hits” the value and this value is updated in real time.
I just noticed that when, as an admin, I edit a post, the “hits” value does not increase while I am editing the post.
When I think about it seems logic because if “hits” value is 15 when I enter in a post edition, it will still be 15 when I finish my post edition because all postmeta are updated when I update a post. SO, even if 10 more people have visit my post my post counter still display a value of 15 when it should display 25.
So now I am trying very hard with my low PHP skill to prevent this field to be updated while I update my posts.
I tried different method but none of them worked :
- I tried to prevent this metadate to be updated before saving my post with the following code :
function exclude_hits_from_update($null, $object_id, $meta_key, $meta_value) {
if ('hits' === $meta_key) {
return true;
}
return $null;
}
add_filter('pre_update_post_meta', 'exclude_hits_from_update', 10, 4);
- I also tried this code in order to try to get the value of the metadate just before updating the post but it did not work either (it seems that this just create more custom field named “hits”) :
function prevent_hits_field_update( $post_id, $post, $update ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if ( isset($_POST['hits']) ) {
$current_hits = get_post_meta($post_id, 'hits', true);
update_post_meta($post_id, 'hits', $current_hits);
}
}
add_action('save_post', 'prevent_hits_field_update', 10, 3);
- I also tried to modify directly the save_post function in the plugin itself (by commenting the line “update_post_meta” but I know it is not enough :
public function adminSave( $post_id )
{
// skip for autosave
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
{
return;
}
// update hits count
if( isset($_POST['post_type']) && in_array( $_POST['post_type'], array( 'post', 'page' ) ) )
{
$hits = ( isset($_POST['hits']) && !empty($_POST['hits']) ? intval( preg_replace( '/[^0-9]/', '', $_POST['hits'] ) ) : 0 );
if( $hits > 0 )
{
$hits_exists = get_post_meta( $post_id, 'hits', true );
if( $hits_exists===false )
{
add_post_meta( $post_id, 'hits', $hits, true );
}
else
{
/*update_post_meta( $post_id, 'hits', $hits );*/ /* HERE */
}
}
}
// clear Popular Posts Widget
$ahc_ppw = new AJAX_Hits_Counter_Popular_Posts_Widget();
$ahc_ppw->clearCache();
return true;
}
Can anyone help me with this ? Unfortunately I can not change the plugin yet because it is linked to several blocks on my website and he use some efficient cache methods.
Thanks in advance for any help !
Jonathan