Count post views in loop

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, consider is_singular().

  • You start the counter with 0, but I think it would be more accurate to start with 1 instead when you use is_single().

  • You can use update_post_meta() instead of add_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.

techhipbettruvabetnorabahisbahis forumutaraftarium24edusedueduseduedueduedueduseduedus