Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Drupal - Get Node Counts For Each Content Type

Category: 

Here is how to retrieve a count of nodes for each content type in a Drupal 6 or 7 site:

SELECT
  COUNT({node}.nid) AS node_count,
  {node_type}.type
FROM {node}
  INNER JOIN {node_type} ON {node}.type = {node_type}.type
GROUP BY {node_type}.type;

(Note, this isn't the 'correct' way to do this in Drupal 7, we should really use the new Database API Layer, but I'm lazy right now.)

This will give you a table something like this:

node_counttype
123page
7article

Here is a copy of the SQL query without Drupal's curly brackets:

SELECT
  COUNT(node.nid) AS node_count,
  node_type.type
FROM node
  INNER JOIN node_type ON node.type = node_type.type
GROUP BY node_type.type;

Comments

Where should i add this sql query.

 

I want my output like, the users and the count of articles he has created in the site

tyler's picture

You'll need to create a custom module, implement hook_menu() to create a page, then place this query in the 'page callback' function. Then you can use something like theme('table', ...); to create a table of user names and their node counts.

Add new comment