Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Rajesh Ashok on
How to get all node ids of a particular content type ?
Comments
I think the only way to do it
I think the only way to do it is to make a db_query looking like this:
This should give you all nids for the content type defined in $node_type.
But if you just want to display nodes with a specific node type, I strongly suggest you make a view for it.
By the way for anyone
By the way for anyone copy/pasting the code above - be sure to put single quotes around the %s. So the code becomes:
Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.
If you have trouble running this example...
If you have trouble running this example and you are using an up to date Drupal 7, try:
Newer versions expect an array of arguments where the index is set to the label you define in the query.
As well db_fetch_object may or may not be in your API depending on version (replaced while(...) with foreach($result as $obj){...}.
Enjoy :)
Why not just: $nids = db
Why not just:
You don't need to loop through the result after executing the query, you can just fetch the column as array, if you have get only one field as result.
Old usernames: pc-wurm, Елин Й.
This worked for me.
This post worked for me. Thanks @hermes_costell.
Using Views module for listout all nid
Views module is suitable for this process.
You will add the following setttings in views:
create one view page (or) block.
set style of view
add field as node-id
set filter as the particular content type
Finally all node id will display for the corresponding content type.
Thanks Guys. I cannot find an
Thanks Guys.
I cannot find an API for this.
It's in Drupal 7....
http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...
In D6 you have to do it the Old Fashoned way.
Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.
Get node IDs or objects In Drupal 7
EntityFieldQuery
Alternatively, this can be done with an EntityFieldQuery:
++Andy
Developing Drupal websites for Livelink New Media since 2008
Extremely useful! Thanks!
This is very useful, thanks! Drop this snippet in and find any content type! Easy, done.
EntityFieldQuery is 10 times
EntityFieldQuery is 10 times slower in this situation, mainly because of the array_keys function.
The fastest query in this case is db_query, however, db_select is the best readable when the query becomes more complex.
Some performance tests were made here, using the timer_start and timer_stop functions.
Drupal backend developer since 2011.
Open Source Webdevelopment Coding Blog
Some good benchmarking there.
Some good benchmarking there. For me it just reinforces what I've always believed, which is why I don't use EFQ.
Thnx!
Thnx!
The lack of performance in EFQ is also the reason I stay away from it.
Drupal backend developer since 2011.
Open Source Webdevelopment Coding Blog
Old Dogs
Ha, I think when I made that comment I had just discovered EFQ and was full of the joys of it. Now I find myself going around telling people they should be using db_select instead ;)
++Andy
Developing Drupal websites for Livelink New Media since 2008
Thanks
Worked perfectly. Thanks..
How to iterate over all the nodes from a content type
Directly through node_load_multiple();
More info at: http://drupal.stackexchange.com/questions/77798/how-to-iterate-over-all-...
Sometimes, we may need
Sometimes, we may need additional fields that the node table does not have built-in. If we want to stick with using nodes, you could query all the node ids of the particular content type and then pass in the results to
node_load_multiple
to get your fully-loaded node object. See below:I think the most compact and
I think the most compact and fastest way to do it:
Old usernames: pc-wurm, Елин Й.
Try this out!
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'yourcontenttype');
$entity_ids = $query->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($entity_ids);
Please doc
Hello, I new with Drupal, I am a Symfony user.
Please, can you provide documentatión for this database interaction?
Thank you!! :)
The database API
The database API documentation for D7 is here: https://www.drupal.org/docs/7/api/database-api/database-api-overview
It hasn't really changed much in D8, so this documentation is still good.
Thanks
Thanks, I will check it!