Share this:

Technorati ReddIt Favorites

Post and user views tracking functions

GD Press Tools tracks number of views for posts and pages, and also tracks users activities for posts and pages. Data is gathered on a daily basis. Here are two functions you can use to get totals of views per post and also views data for each user and post.

Getting post views

First function is:

presstools_get_post_views($post_id = 0)

If $post_id is set to zero, function will attempt to get post ID from current post. If this fails, error will be returned. If it’s OK, object with views information for that post will be returned. This object contains these properties:

  • post_id: post ID
  • users_views: number views from users
  • visitors_views: number of views from visitors
  • total_views: total number of views

Getting users post views

First function is:

presstools_get_user_views($id = 0, $all_posts = false, $post_id = 0)

First parameter is ID for user. If this is set to zero, plugin will attempt to get ID for current user. Second paramter set to true will return array with views for all posts for user. If parameter remains false, plugin will get views for post specified with third parameter. If this one is zero, function will attempt to get post ID from current post. If any problem occures with paramters, function will return error.

Here are some examples:

presstools_get_user_views(11, true);
This will return views for all posts for user with ID 11.
presstools_get_user_views(31, false);
This will return views for current post for user with ID 31.
presstools_get_user_views(31, false, 4);
This will return views for post with ID 4 for user with ID 31.

14 Responses to “Post and user views tracking functions”

  1. Arpit | May 1, 2010 at 20:24

    Thanks Milan for this function. I tried to use presstools_get_post_views function in single.php but no luck. I did something wrong. Could you please send me sample code?

  2. Arpit | May 2, 2010 at 21:33

    Wonderful. Works very well on my blog. Check out http://arpitshah.com

    Thanks again. :)

  3. Neosin | August 2, 2010 at 05:45

    I just can’t work out the code to show total views… Could you please post example code to do this?

    • MillaN | August 2, 2010 at 10:13

      All the code you need is in the post. I don’t understand what more do you need, it’s very simple.

      • neosin | August 2, 2010 at 10:15

        When i put that code in, the site goes white (blank).

        What would be the code to see total visitors.

        Because just copy/paste of that code doesn’t work for me. Am I missing something? Just putting that top line into PHP tags doesn’t do anything but blank the site page.

        • MillaN | August 2, 2010 at 12:41

          Do you used GD Press Tools plugin? It’s needed for this to work, WP doesn’t track anything, GD Press Tools does that.

          • neosin | August 3, 2010 at 08:24

            Yes of course. I’m using GD press tools…

            It’s a rather simple question, if you was going to put this into a page what would be the code?

            I get that you posted the code up top, but simply putting that into some php tags doesn’t do anything.

            I want the total number of current post views.

            phptag code to use endphptag

        • MillaN | August 3, 2010 at 09:42

          Functions above work, I guarantee you. And if you read what the text says: function WILL RETURN, and that means that function result will be number of views. You need to use some of the print functions to display that, like `echo`.

        • Arpit Shah | September 22, 2010 at 06:21

          Try this:
          total_views;
          echo $view1;
          ?>

          I’m using it here: http://arpitshah.com/sitemap/

  4. Spewf | September 10, 2011 at 02:24

    Is there any way to sort posts by most views? A lot of plugins have this functionality but it doesn’t seem like GD does….

Trackbacks/Pingbacks

  1. GD Press Tools: Show Views/Tracking - netCrunched - January 20, 2012
  2. GD Press Tools: Show Views/Tracking - iCrunched.co - September 14, 2012

    [...] reading: http://www.dev4press.com/2010/tutorials/plugins-tutorials/gd-press-tools/post-and-user-views-trackin… jQuery(document).ready(function($) { [...]

Leave a Reply

Follow us on Twitter

Date Archives

Social Networks

Google Plus Profile Subscribe to RSS feed LinkedIn Profile Follow me on Pinterest Watch our Youtube Videos Find us on Flickr

Feedburner Feedburner updates

Sign up to receive news from this website to your email.

Recent Blog Posts

Dev4Press on YouTube

Share This

Technorati ReddIt Favorites