1. Add the following function in your theme template.php
function <theme>_get_user_nodes_count($uid) { $query = db_select('node', 'n'); $query->condition('uid', $uid, '='); $query->condition('status', '1', '='); $query->addExpression('COUNT(1)', 'count'); $result = $query->execute(); if ($record = $result->fetchAssoc()) return $record['count']; return 0; }
2. So you could retrieve the node count by calling
<theme>_get_user_nodes_count($uid)
If you only want to count the number of nodes of a specific content type use the following piece of code in template.php instead
function <theme>_get_user_nodes_count($uid) { $query = db_select('node', 'n'); $query->condition('type', '<content-type>', '='); $query->condition('uid', $uid, '='); $query->condition('status', '1', '='); $query->addExpression('COUNT(1)', 'count'); $result = $query->execute(); if ($record = $result->fetchAssoc()) return $record['count']; return 0; }
Done =)
Next: Drupal 7 – Get the number of comments by user
Reference: Drupal Forum – Post count (x)/comment count (x) on user profile page
Reblogged this on Van Lian Hup and commented:
Drupal 7 – Get the number of nodes by user
LikeLike
Hi, I need to display the count of nodes by each user, for three separate content types. For example, User A has 2 Articles, 4 Blog Posts, and 9 Reviews. How can I adjust the code to count each node type? Once the code is adjusted, how would I print those values as part of a Views-driven grid of users?
LikeLike