0

Is there any currently supported plugin to display the number of page views on a post? wp-postviews seems to be the most commonly used, but it requires modifying the theme (which would require creating a child theme to avoid being overwritten by theme updates), and does not seem to work with caching plug-ins.

It seems like this should be straight-forward to extend the wp-statistics / jetpack API to display this information, but I haven't found a plugin that will do so. Seems like a common feature, surely this has been done somewhere?

3 Answers 3

3

Try this Works on single site

// post views
function setAndViewPostViews($postID) {
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
    return $count; /* so you can show it */
}

You need to add the following function to your theme's functions.php and call it inside the loop or inside your posts.php or page.php of your theme. like so

<?php echo setAndViewPostViews(get_the_ID());  ?>

This will increment and persist the post's count then show the number of views.

Got inspiration from the question and did a write up if it might help anyone http://nerudo.mregi.com/how-to-display-number-of-page-views-on-a-post-wp/

2
  • thanks - can you provide more details? Which file are you editing? Does this require plugins? I assume you need to then create a child-theme to avoid these changes being over-written? Thanks.
    – cboettig
    Dec 6, 2011 at 16:51
  • Simple yet effective! Aug 8, 2018 at 8:50
1

wp-postviews seems to be the most commonly used, but it requires modifying the theme (which would require creating a child theme to avoid being overwritten by theme updates) ...

If you want to actually display the view count on the post but this item isn't built in to your theme already, then you're going to have to edit your theme or create a child theme. There's no way around that.

...and does not seem to work with caching plug-ins.

If you're wanting to display a dynamic view count on a post, using caching isn't necessarily a good idea.

Most caching systems return static HTML content in place of a dynamically-generated page. This means the counter wouldn't increment with each page load unless you force the cache to refresh at a set interval ... and then the counter would only chance when that refresh occurred.

Seems like a common feature, surely this has been done somewhere?

It has, and you've already found one of the ways people have been doing it - WP-PostViews

There are a few other plugins out there that do the same thing, since you've already found one I assume you can find the others pretty quickly as well.

2
  • Thanks for this reply, was definitely helpful. I am confused though - other plugins display content on pages without requiring a child theme - (e.g. kcite, post revision display). It seems the plugin Ajax-the-Views addresses the caching issue. Would be nice if wp-postviews or other option used the wp-stats api so it would match those statistics, rather than starting at zero.
    – cboettig
    Nov 17, 2011 at 16:35
  • Most plugins that display content will hook in to the content filter or another filter in WP and inject HTML markup directly in the page. Some times this works just fine. Other times it can cause the layout of your site to explode. It all depends on what theme you're using and how it's coded. The safest route is to use a child theme so other developers aren't blindly guessing where the markup will fit with the rest of your site.
    – EAMann
    Nov 17, 2011 at 17:16
0

Using WP-PostViews with Ajax-the-Views and a child theme for both the index.php and single.php files of my theme is the best solution I have at the moment.

Pity that this does not call the wp-stats API with my API key, so that the counters would not start at zero but reflect the history stored by the stats plugin.

Thanks @EAMann for suggestions, and happy to hear of other solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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