0

I want to update a custom field value whenever an Editor visits a post. I want to get it done to track hits on a post by editors on my site. Not finding a proper solution or I am confused with multiple solutions. I think I can use update_post_meta() on single post page but not sure if it is a right solution.

  • 1
    I know, it's not a good solution or assistance, but with lack of time, I can say, you can take code-assist from "Popular Posts" plugin to track how to update db on user-view. Then try to narrow the broadness of the query with and if() condition with current_user_can('editor'). Hope that will light you to a good solution. – Mayeenul Islam Aug 1 '13 at 13:56
  • but i dont want to use any plugin for this functionality,it will be a theme feature – Vikram Aug 1 '13 at 14:33
  • @Vicky he was suggesting you look at how that plugin does this, so that you may write your own solution, not use that plugin itself. – Milo Aug 1 '13 at 15:57
1

Something like this should do:

function track_editor_wpse_108606() {
  if (is_single()) {
    $current_user = wp_get_current_user();
    if (user_can( $current_user, 'editor' )) {
      global $post;
      $current = get_post_meta($post->ID,'editor_tracker',true);
      $current = (!empty($current)) ? $current + 1 : 1; 
      update_post_meta($post->ID,'editor_tracker',$current);
    }
  }
}
add_action('wp_head','track_editor_wpse_108606');

This is not especially efficient. You are reading and writing to the database for these page loads. You could improve this by means of a direct SQL query if you want to go that somewhat risky route, and could improve it further by using Javascript to insert the value if you don't mind that it won't work if the user disables Javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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