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.
Related Posts:
- How to Change Loop to Order Posts by Views (using wp-postviews plugin)
- How to add “time” data this?
- Double count view in archive.php
- How can I make the loop not count special post formats towards the post count?
- Apply an Incremental Counter in an Array Function
- How do I display an image before the first post of the loop when I’m using get_template_part?
- Problem with displaying HTML content after in_array [closed]
- Add “showing posts x to x of y” in custom post type paginated loop
- Give each posts in a loop a number in sequence
- Loop is crazy – one row displays wrong count of posts [closed]
- How to sort a loop after most viewed
- php loop error for count posts in category
- Custom Looping of WordPress Posts
- Link Button url count php and wordpress
- Counting the posts of a custom WordPress loop (WP_Query)?
- Get post content from outside the loop
- Is there any difference between the_title() and echo get_the_title()?
- Using the Loop to show all levels of subpages under a parent page? Halfway there
- Cleanest Way to Select Every Second Element in a Loop?
- Pagination not working on home page
- is_home, and is_front_page conditional problem
- Is `query_posts` really slower than secondary query?
- Are there any scenarios where the query_posts may be used?
- Display subpages under parent page as a list within a loop
- When to use wp_reset_postdata();
- Why do themes have `while( have_posts() )` in templates like single.php?
- Count singular post views automatically
- Related Posts loop – offset
- Too many nested wp_query loops in this hypothetical query?
- pop-up lighbox with AJAX
- WordPress Loop Through Particular Pages
- Showing current subcategory
- Display date only once for a each set of posts relating to that date
- WordPress Loop – Style rows of posts differently
- Generating dynamic Tabs with multiple query post loop
- How do I override part of the main loop in my child theme?
- Chopping up a large ajax request of around 200 posts [closed]
- Combining the_excerpt with the_content
- Custom loop with conditional statement to separate each terms
- How to get an array of years from all of the search results, and use it to filter by year the paginated loop?
- Only display videos and images in the_content() within loop
- Get search.php results in header.php?
- Recent posts on a static home page
- Query last updated posts (posts updated in the last 24 hours)
- I write simple wordpress loop for taxonomy.php file but don’t show result
- Publish Categories for a particular Author on a given Post
- How to hook into container
- Exclude first post (sticky or not) from the loop using query_posts()
- How to add a specific div element to the first post in a row?
- Need to add class to first element in a foreach loop
- Scrolling Posts with Ajax
- Sort posts by first letter in the title, not by first number
- something strange is happening to my code
- How to modify search.php to not show pages only show posts
- facebook like, tweet and google+1 buttons inside the loop on home, archive and search pages and missing facebook like buttons
- Loop Offset for tag based “Related Posts”
- Show articles in Pair
- How do I put my frontpage featured image in a page template for home.php?
- Randomly display post based on meta value. (Kind of Priority/Weight)
- Make a loop to return x number of posts, but only if they have content or excerpt
- How to print redux gallery feilds id? [closed]
- Why the first with flexslider appears as blank on my code?
- Rendering pages
- How can have a conditional template tag based on the main loop within a secondary loop with new WP_Query(), using get_template_part()
- How to render the single post template with the post name?
- WP_Query is only looping home page, not pages
- How to filter posts by category INSIDE the loop
- static page with multiple loops breaks post pagination
- new custom theme – posts displaying on top of one another
- Help with if and else statement
- Building a form with WordPress, and also pulling that data for a user page
- Adding a second loop breaks everything
- Custom Query with Sticky Posts
- Count singular post views automatically
- Author.php Page views counter
- is it bad to copy the loop for a template?
- I want to load post in bootstrap Modal dynamically
- Why my loop is repeat?
- Excerpts not showing on main page
- Return only one post format in index.php [duplicate]
- How to display just video post format?
- Is it possible to get a post ID of a 2 different custom post types loop embedded in the same post to perform update_post_meta from the frontend?
- Loop shows post 5 times in a query
- How to execute custom loop before loop content?
- Make custom WordPress comments list
- Template included with plugin doesn’t return all posts for custom taxonomy
- Sorting posts to make those with post thumbnail show up first
- Best way to check if a post with specific meta exists
- Return only content in excerpt field in loop
- Default Template infinite loop of Title
- get_posts and wp_autop (remove filter)
- Trouble preventing duplicate posts between 2 loops on homepage
- Change the way wordpress outputs images or image galleries
- How can I access ‘key’ and ‘value’ from an array in a for loop?
- How to get current page id and compare it to looped pages inside the same page?
- posts_nav_link() not working on index.php
- Error in Wordprewss loop for page template [closed]
- Tags outside the loop
- wp_mail sending only once inside foreach loop
- How to use orderby on meta_value when using Pods custom database table storage