Is there a function that will count nodes of a specific content type?

I'm trying to code a 'stats' block and I'd like to display: Total videos: 12, Total Events: 43, etc.

I don't mind querying for this, if there is no function. I started, but...

$countresult = db_query("SELECT n.type FROM {node} n");
$countobject = db_fetch_array($countresult);

Then what? I'm stumped.

Thanks in advance.

-Jesse

Comments

pwolanin’s picture

to find the count of nodes of type 'blog':

$count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = 'blog'"));

for example, to show in a page snippet the count of nodes of each type:

  // for Drupal 4.7.x
  $node_types = node_get_types();
  print("<ul>\n");
  foreach ($node_types as $type => $name) {
    $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
    print("<li>$name : $count</li>\n");
  }
  print("</ul>");

---
Work: BioRAFT

apt94jesse’s picture

Works like a charm, thanks!
-Jesse
wrestlespace.com

droople’s picture

I got a blank white screen after using this code to get the number of audio files on my site. Any help? I can't login into site anymore

<?php
  // for Drupal 4.7.x
  $node_audio= node_get_audio();
  print("<ul>\n");
  foreach ($node_types as $type => $name) {
    $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = 'audio'", audio));
    print("<li>$name : $count</li>\n");
  }
  print("</ul>");
?>
lenkkivihko’s picture

1) Log on

http://localhost/?q=admin/ (replace localhost with appropriate sitename)

Log on directly to your admin ui and delete the node with errors.
Next time please try code at your local host.

2) Errors

I would try following places:
->$node_audio= node_get_audio();
is the audio really required in here - why not type?

-> $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = 'audio'", audio));
The last audio, shouln't that be $type?

Just wild quesses....

droople’s picture

I cannot access any page, all I get is a blank white screen.

thanks

lenkkivihko’s picture

PHP error causes white screen. I would suggest to log on directly to admin ui -> The error node is not rendered.

Unfortunate if the proposial did not work.... I've used it a lot...

Then it's not the one error node that causes the issue. Better dig a bit deeper...

droople’s picture

Thanks all

I went into phpMyAdmin and set all blocks to status (0)

that worked well, so I could log in then from there I deleted the custom block

Now that code up there shows all nodes with the same count from audio

How do I show just one node type eg "audio"

cheers

apt94jesse’s picture

The above code works great for me... You have a few anomolies in the code you pasted though.

<?php
// for Drupal 4.7.x
$node_audio= node_get_audio();
print("<ul>\n");
foreach ($node_types as $type => $name) {
   $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = 'audio'", audio));
   print("<li>$name : $count</li>\n");
}
print("</ul>");
?>

Your "$count" line should read the following:

   $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = 'audio'"));

You don't need the last "audio" in there, unless you were entering "type='%s'", since that would reference an input string at the end of the query. That's why, in the above example, he sets a $type variable, then uses that as the input string at the end of the query.

This works like a charm for my 4.7.4 install at wrestlespace.com. It's on the homepage if you want to see. Also, make sure that "audio" is the exact content type name, and not something like "content_audio" like CCK creates.

Good luck.

-Jesse

Notice how

droople’s picture

Would you mind sharing with me your user snippets. I like the layout on your wrestling site

Great job

apt94jesse’s picture

Thanks, its custom straight from bluemarine. I did a WHOLE lot of hacking the page.tpl.php page, because it was a lot faster than learning some of the advanced theming functions of phptemplate, and a lot more flexible.

What snippets do you want? I don't mind sharing, but I honestly may not have time to break them out and explain them for other sites, as they are mostly hardcode for my domain and setup. But I have no problem sharing and cut/pasting anything. Let me know what you'd like.

droople’s picture

I liked the way your profile list is displayed, 1st with just the name and profile picture, then the full profile details

Could you share that please

thanks

orthopoint’s picture

How can I show the number of users who are subscribed to my site ?

pwolanin’s picture


$num_users = db_result(db_query("SELECT COUNT(*) FROM {users} u WHERE u.access != 0 AND u.status !=0"));

print ($num_users ." active users are registered at this site");

---
Work: BioRAFT

orthopoint’s picture

Thank you

j0k3z’s picture

Is it possible to only count nodes for a certain user?

On my profile pages I want to create a box called "My Contributions" and print out how many blogs/images that person has added to the website.

How would I do this?

j0k3z’s picture

and will this work with Drupal 5?

latte’s picture

This would be a great block to have on my site. Anyone know how to do this?

Latte/

tillmanj’s picture

What changes to the above code would be needed for 5.0? When I used it in my 5.o site it printed out:

  • Object : 1
  • Object : 0
  • Object : 0
  • Object : 1
  • Object : 28

without node names...

drupaloSa’s picture

 <?php
  // for Drupal 5.5
  $node_types = node_get_types('names');
  print("<ul>\n");
  foreach ($node_types as $type => $name) {
    $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
    print("<li>$name  : $count</li>\n");
  }
  print("</ul>");
?>
Fayna’s picture

That code works great in 5.6, but is there a way only to include certain content types?

drupaloSa’s picture

it is possible to add an if statement just before the print, such as

<?php
  $node_types = node_get_types('names');
  print("<ul>\n");
  foreach ($node_types as $type => $name) {
    $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
   if (!strcmp($type, "type_your_content_type_here"))
    print("<li>$name  : $count</li>\n");
  }
  print("</ul>");
?>

But there may be better solutions using some parameters with node_get_types. I couldn't figure out this last one unfortunately.

Fayna’s picture

if anyone is interested, Nancyw helped me with the code for displaying the number of nodes of a certain content type. See her comment here. It works in a block, too. :)

Rosamunda’s picture

Just suscribing for this, because it´s just what I was looking for!
Thanks!

Rosamunda
Buenos Aires | Argentina
www.ligadelconsorcista.org

Summit’s picture

Hi,

Would it be possible to make a stats-block like this, in which I see which users ads how many of the nodes? And may be also the last date per user he or she added a node?
I need this, because I pay for a sort of development aid people for adding nodes to a system.I believe in economic development aid, for which they have to do something economical for their money, but off course it costs me more than I benefit from it in this project..:(
Thanks for your reply on this!

Greetings,
Martijn

anthonym’s picture

I've been working on customizing the audio module to display according to my preferences. I found this statement in the audio_feeds.module that seems to test for whether or not audio files are attached to a node (of course I could be wrong about that!). I'd like to insert it in a block of code for an audio player so that it will show up only if a node has audio files attached. At the moment the player appears on all nodes, whether or not they have files attached. If I insert the below line of code, then the audio player doesn't display at all, so obviously I'm doing something wrong. Any ideas?

  if (variable_get('audio_feeds_attach_'. $node->type, 0) && count($node->audio_attach) > 0) {

//audio player defintions...

}

Anthony

Rosamunda’s picture

I know it´s an easy snippet, but it happen that I don´t know how to do it, and the code that i´ve found out there in the forum jungle ;) is for 4.7 and doesn´t work in 5.x...
Thanks!

Rosamunda

mr_dimsum’s picture

Great code. I haven't tried it out yet, but I was wondering if it was possible to count the nodes of a specific content-type made by a specific user? I'd imagine this would be a great contribution to have beside the "Author Information" block!

parrottvision’s picture

yes - I would also love to have that.

maurosilva’s picture

This function for Drupal 6.x have a few changes...using $type , not $name, avoid problems...

  // for Drupal 6.x
  $node_types = node_get_types();
  print("<ul>\n");
  foreach ($node_types as $type => $name) {
    $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
    print("<li>$type : $count</li>\n");
  }
  print("</ul>");

Bye

mauro--

tribsel’s picture

...then add this AND status = 1 into query.

$count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = 'contenttype' AND status = 1", $type)); print($count);

iamnayr’s picture

Is there any way to grab the number of nodes of a certain type that the current user has yet to read? For instance, when a user logs in, there is a link to "news" with a small badge icon over it with the total number of unread posts for the node type "blog". Once they click that link and view any number of posts, the number on the badge declines until eventually reaching 0.

poissanb’s picture

A Block showing the counts of nodes (by type) that a user has subscribed to, have been updated or had additional comments added to them, since their last login.

BassPlaya’s picture

This is great info! No need for a module but simple a MySQL query! Great! Thanx!

Authentically,
BassPlaya

pepe84’s picture

You can do the same in one database query:

    $html = "<ul>";
    $result = db_query(
        "SELECT type, COUNT(*) as count FROM {node} GROUP BY type"
    );
    while ($r = db_fetch_object($result)) {
        $html .= "<li>{$r->type} : {$r->count}</li>\n";
    }
    $html .= "</ul>";
    
    return $html;
hunanka’s picture

I would love that

these codes are not the node types, node names should appear

Could you give the code in the node names, please

thanks

gMaximus’s picture

I wanted the content type name too... also only certain content types to be shown...

So i adjusted the code provided by Pepe and came up with this:

<?php
    $html = "<ul>";
    $result = db_query(
"SELECT name, COUNT(*) as count FROM {node},{node_type} 
where node.type = node_type.type
and name in ('content type 1','content type 2')
GROUP BY node.type"
    );
    while ($r = db_fetch_object($result)) {
        $html .= "<li>{$r->name} : {$r->count}</li>\n";
    }
    $html .= "</ul>";
    
    return $html;
?>

Just need to replace the example content type names to your ones...

Hope it helps others....

Guy

I am always interested in paid work... Contact me through Online Business Builders

Jan-E’s picture

Should not the second and third 'node' be {node} and the second 'node_type' {node_type}? Probably your table names are node and node_type without a prefix. But with prefixed table names the code fails.

BTW: you can enter the query in phpMyAdmin as well (with the prefixed table names).

rroose’s picture

Great, thanks! Is there also a way to limit this to a taxonomy term?

web2get’s picture

/*
*** Drupal 7
*/  
  $result = db_query("SELECT COUNT(*) FROM {node} WHERE $type")->fetchAssoc();
  print $result['count(*)'];
BeaPower’s picture

What about for users - specific node counts. Im having an issue showing this through views aggregation for 1 out of 8 content types, dont know why.

prezaeis’s picture

for drupal 7 version, how do you specify the node type to count?
also how can we show total comments on the site that have been posted?

tryitonce’s picture

... very useful thread and nice to see how an old story gets updated ....
btw. I noticed this along the search - but found what I needed here - so I thought I can at least mention it for others - http://drupal.org/project/term_node_count .... counting terms - taxonomy ....

prezaeis’s picture

Can someone help me here on Drupal 7?

None of these codes seem to work, im trying to print a small line on the user-profile.tpl.php page

I have a block that prints certain information about the users profile being viewed, i want to print the TOTAL NUBER OF NODES that user has created (of all types), and the NUMBER OF TOTAL COMMENTS posted by others on HIS nodes..

Any ideas?