How to check whether any node is accessed in past or not

  • 2 November 2012
  • mohit.aghera

During one of my project development I need to check whether in past any node is accessed by logged in user or not.

Fortunately node module and “history table of database ”came to help me.

In node module there is function  node_last_viewed($nid) which returns the last unix timestamp of node access time.

Basically I was using Og forum module so I wanted to check whether any forum topic is viewed previously by logged in user or not.

So first I filtered all the groups of the logged in user.

After that I checked field_data_group_audience table which stores the relation between group node and forum topics in that node.

So I just accessed all the forum nodes of group in which user is member and checked the time stamp.

function get_latest_unread_nodecount() {

       global $user;

       $unreadcount = 0;

       $query = '';

       $usergroups = og_get_groups_by_user($user);

       $query = db_select('field_data_group_audience', 'f');


       $query->condition(db_or()->condition('f.bundle', 'forum', '=')->condition('f.bundle', 'post', '='));

       $query->condition('f.group_audience_gid', $usergroups, 'IN');

       $result = $query->execute();

       $rows = array();

       foreach ($result as $row) {

              $viewstatus = node_last_viewed($row->entity_id);

              if($viewstatus == 0) {




       if($unreadcount > 0) {

              return $unreadcount;


       else {

              return 'No';



$unread count will fetch the latest unread forum posts for the group in which user is member.



Pharmd735's picture

I truly appreciate this post. I've been looking everywhere for this! Thank goodness I found it on Bing. You've made my day! Thank you again! eaacekdadddd

cathgiya's picture
skinner's picture

Thank you for the information. I really appreciate this post.

I've learned something this day.. :)  Kindly visit this site..

sherlin decosta's picture

Thank you for taking the time to publish this information very useful! I’m still waiting for some interesting thoughts from your side in your next post


Mass Effect 3 Jacket


al3abbanat's picture

thanks for this great topic العاب بنات


Add new comment