Few remarks:
-
You should consider using a prefix on your meta key, for example
lars_views
, to minimize the risk of other plugins messing around with it. -
If you want to hide it from the edit screen, you can use an underscore:
_lars_views
. -
Consider wrapping the part of your code, that updates the counter, with
if( is_single() ){...}
. This will restrict it to single post views. More about this conditional tag in the Codex. If you have custom post types, consideris_singular()
. -
You start the counter with
0
, but I think it would be more accurate to start with1
instead when you useis_single()
. -
You can use
update_post_meta()
instead ofadd_post_meta()
, if you want to simplify your code. Check out the Codex, -
I would rather have a seperate function to show the counts and another one to update the counter via some hook. I think it’s more flexible.
Hope this helps.