Support » Plugins and Hacks » Hacks » How to track post views by logged in user? (using post_meta)

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator t-p

    (@t-p)

    ake a look at this WP-Snippet, you might be able to tweak it to fit your needs: http://wp-snippets.com/post-views-without-plugin/

    MickR

    (@mickr)

    Hi t-p, thanks for the response. Had a look at that and it’s not really what i’m after. The difference being that that’s a simple count updating the same individual postmeta record each time a post is viewed.

    What i’m trying to achieve is to store the IDs of the last 50 unique logged in users who view each post. The owner of a post will then be able to see which other registered users have visited their post. (In this particular application each registered user will only be allowed to create one post).

    I’m bouncing between possibly using duplicate keys in the postmeta table, or alternatively creating/using a new DB table as a solution – if i could work out whether it’s possible to update the oldest of postmeta entries that have duplicate keys i’d be one happy chappy!

    Moderator bcworkz

    (@bcworkz)

    How about just letting the table grow beyond 50 entries, temporarily. Then you just have to log each access in turn. Then once in a while, schedule a task to go in and delete the older records so that the table is only 50 records long again.

    MickR

    (@mickr)

    Hi bcworkz,

    You’re right, may be the way to go, and possibly using a cron job to do the tidying up.

    One thing i’m not clear on though is whether you’re thinking add to the wp_postmeta table, or possibly creating a new table for the purpose.

    If a new table, would you have any thoughts on the choice between one new table per user (post), versus one overall table where all visit stats are stored for every user.

    If i’m storing 50 records for each user, a single global table could have anywhere from, let’s say, 500,000 records upwards. Whilst adding a record for each post view doesn’t have any real overhead, i’m wondering if retrieving the data (50 records spread across the table) for a user to see who has viewed their post could be processor heavy?

    Obviously i’m still bouncing thoughts around here, but appreciate the feedback.

    Moderator bcworkz

    (@bcworkz)

    Umm… I wasn’t thinking that far into it, to tell the truth. Now that you bring it up, for that kind of scale, definitely create your own table to contain this data. But I think creating a table for each post would highly impractical. My understanding of mySQL is that reading data is relatively light on resource in comparison to writing data, though writing one row obviously is not a big deal. But neither is reading 50 divergent rows. The magic of a good indexing and caching system I guess.

    yunieskid

    (@yunieskid)

    I am trying to get the posts viewed by a certain loged in user, I have how to count the number of views in a post, but i can’t retrieve the count of views by an given user.
    What I need is to have a register of the amount of posts by a user, to identify the category of those posts and then have an idea of the favorite category for the loged in user, and then in the home page, show to the user the posts belonging to that category.

    Any help for solving this problem?.

    Moderator bcworkz

    (@bcworkz)

    @yunieskid, you should really start your own topic so your question gets maximum exposure. People that may be able to help you often skip over threads with several responses because such threads have usually devolved into a two way conversation. The forum admins also prefer you do this because it helps with their own statistics collection.

    To address your question anyway, there’s a number of possible approaches. I personally would probably first try inserting a function call on every applicable template. That function would add a row into a table specifically setup to accumulate such data. You could then make specific mySQL queries to calculate whatever statistics you need.

    yunieskid

    (@yunieskid)

    @bcworkz, thanks for your reconmedations related to the way i have to make my questions, as a matter o fact i did also post the question as a new topic, i’ve been searching in other sites and i didn’t find so much, and i did just make something similar to what you told me, and what i am going to do is to create a table in the database with 3 columns, the current user ID, the category of the post that is visiting and the amount of times that the user visits posts with that category, USER_ID, CATEGORY, COUNT_VISITS, so i will be able to determine what is the more visited category by each logged in user, and later perfomr what i need.

    MickR

    (@mickr)

    hi bcworkz,

    Thought i’d drop back in and let you know how i got on.

    In the end wrote a plugin to address the need. Plugin creates a new DB table on activation and I opted for a column based solution. Long and short of it is that for each new post 50 rows are automatically created. One of those is an auto-updated timestamp and i’m using that to roll round and update the oldest entry when the 51st unique visit takes place.

    Plugin also has a shortcode function which displays the stats when called. (Avatar of person who visited, how many times they visited the post, the last time they visited, and a link to their profile).

    In test mode and so far so good 🙂

    Moderator bcworkz

    (@bcworkz)

    @mickr, thanks for the update. Sounds like a great implementation to meet your needs. I enjoy watching an idea get bounced around and develop on sites like this. I’m confident you will have continued success.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to track post views by logged in user? (using post_meta)’ is closed to new replies.