You could hook into template_redirect and execute a helper function:
add_action( 'template_redirect', 'wpse_75558_count' );
function wpse_75558_count()
{
if ( is_singular() )
setPostViews( get_the_ID() );
}
To display the post views, you have to use a later hook. I would recommend the_content:
add_filter( 'the_content', 'wpse_75558_show_count' );
function wpse_75558_show_count( $content )
{
$count = getPostViews( get_the_ID() );
if ( ! is_numeric( $count ) )
$count=""; // prevent errors showing to visitors
else
$count = "<p class="view-count">Views: $count</p>";
return $content . $count;
}
Just an idea, I haven’t tested the code.
Related Posts:
- Count singular post views automatically
- Action hook ‘wp’ firing twice… why?
- Alternate custom content in the loop
- Custom function for “Submit for Review” hook
- How can I hook into creating a new post and execute wp_die(), before the post is inserted into the database?
- Double count view in archive.php
- How can i do something after head like adding a hook for after head but before post
- Adding buttons to Add New Post and Add New Page
- How to access $post from a callback function
- Copy post to separate database with “add_action(….)”
- Call Web Services on post first publish
- What does WordPress do if I save a post without content/title? [duplicate]
- Divs appearing everywhere in post content
- Automatically set post_parent value
- Is there a way to know when a page has been updated and do some action only once?
- Is update_post_meta used when save_post action hook is invoked?
- Retrieve posts inside foundation tabs and tab-content split by 3 per row
- Alternative content between posts no repetition
- Grab meta data before post is saved
- How to control Post view count incresing in sidebar widget posts too?
- How to get total posts count for each date?
- Modify upload directory to use post category slug in file path on multisite installation
- Comment count next to post title?
- How to mark every 3rd post
- Is there a action hook for the “Empty Trash” button?
- How to get posts published between a date and today?
- Get first post from wp_query
- Get old values for post before saving new ones
- Execute function when post is published
- Create posts on user registration
- get comments and get posts in loop
- Why do I need to use The Loop on pages (inside page.php etc)?
- Set font size automatically according to number of words in post
- Display All Sticky Post Before Regular Post
- Can’t show comments count per post outside loop
- Specific loop in Shortcode
- Filter post before *editing*
- Redirect based on user post count
- Repost post on specific date every year
- Search widget breaks when using multiple loops?
- Show the author’s count of total amount of posts assign to a custom taxonomy term
- Auto Refresh Post List after X seconds
- Display posts with comments closed, with pagination?
- While in “the loop”, detect if a post is the most recent
- Contact form 7 post loop [closed]
- How to generate numbers indistinguishable for the IDs of the posts
- If Specific Post, Link Elsewhere
- How to filter posts that belong to a specific category only if that is the only category
- Most recent post from another database
- page.php showing loop of posts and not page content
- Child_of not displaying all children posts.. via get_posts
- Which WordPress hook fires after post content loaded?
- Trying to use AngularJS with WordPress without any API
- Pagination with an array of post objects?
- Set static page/post from another blog on same network
- wp_query random post
- Display Posts on Custom Page
- Possible to alternate between two loops
- How to add category post count in main navigation menu [closed]
- Multiple Loops Breaking Pages
- Counter is skipping post when I still want it visible [closed]
- How to only publish posts with image in it
- Hook for changing excerpt content when excerpt not set
- Optimizing the blog loop
- Accessing Post ID Within Loop
- Different number of posts per row in grid layout
- linking to post outside the loop
- Defining ‘last’ class on foreach blog posts
- Actions to use when flushing cache (when posts are added / deleted / modified)
- Want to change the post every three days with in specific category
- Author post count in category
- Show post count in custom taxonomy page
- How to count posts in tags within time limit
- Loop and output 4 rows of posts on home page
- Loop through all posts showing duplicates
- Post being duplicated with foreach loop
- Custom post type – Loop out in random order but same 15 to appear first… still random
- WordPress infinite post cycle
- List Authors For Current Category
- Hook To Get All Posts Deleted
- Display the latest post from some selected authors
- How to show the posts list into a static page? Problems to use the loop into a static page
- How to use extract shortcode in loop?
- Adding a category at even positions on main loop with modified pagination
- wordpress taxonomy results
- Displaying posts on Homepage
- Where are my posts linked to? I keep getting a Not Found Error, and I have no single.php page
- The loop: more posts than I want to fit on one page
- WordPress posts loop pagination – first page return 125 posts instead of 10 and the rest return 10
- The post order is different for logged-in and non-logged-in users? [closed]
- save_post not working
- How do I show the post title if an advanced custom field hasn’t been used?
- Adding Count Post in Page
- Custom post order returning posts from other categories
- CSS code for “inserting a custom post divider” after each post
- Count words for all posts by all authors
- Word count for all posts of all authors
- Display post count on archive page in reverse order
- WordPress Ajax load doesn’t work
- WordPress current post functions inside loop doesn’t work anymore, are they incompatible with php 8.0?