How to get all node ids of a particular content type ?

Comments

Cheesebaron’s picture

I think the only way to do it is to make a db_query looking like this:

$result = db_query("SELECT nid FROM node WHERE type = %s", $node_type);

$nids = array();
while ($obj = db_fetch_object ($result)) {
  $nids[] = $obj->nid;
}

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.

hermes_costell’s picture

By the way for anyone copy/pasting the code above - be sure to put single quotes around the %s. So the code becomes:

$node_type = "YOUR_NODE_TYPE_MACHINE_NAME"; // can find this on the node type's "edit" screen in the Drupal admin section.

$result = db_query("SELECT nid FROM node WHERE type = '%s' ", $node_type);

$nids = array();
while ($obj = db_fetch_object ($result)) {
  $nids[] = $obj->nid;
}

Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.

RealityBitesYou’s picture

If you have trouble running this example and you are using an up to date Drupal 7, try:

$node_type = "YOUR_NODE_TYPE_MACHINE_NAME"; // can find this on the node type's "edit" screen in the Drupal admin section.

$result = db_query("SELECT nid FROM node WHERE type = :nodeType ", array(':nodeType'=>$node_type)); //<-- change 1

$nids = array();
foreach ($result as $obj) {  //<-- change 2
  $nids[] = $obj->nid;
}

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 :)

Elin Yordanov’s picture

Why not just:

  $nids = db_query("SELECT nid FROM {node} WHERE type = :type", array(':type' => $node_type))
    ->fetchCol();

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, Елин Й.

AmolB’s picture

This post worked for me. Thanks @hermes_costell.

roger.ajith’s picture

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.

Rajesh Ashok’s picture

Thanks Guys.

I cannot find an API for this.

hermes_costell’s picture

Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.

othermachines’s picture

// Return all nids of nodes of type "page".
$nids = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->fields('n', array('type'))
    ->condition('n.type', 'page')
    ->execute()
    ->fetchCol(); // returns an indexed array

// Now return the node objects.
$nodes = node_load_multiple($nids);
kingandy’s picture

Alternatively, this can be done with an EntityFieldQuery:

  $query = new EntityFieldQuery();
  $result = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'mycontenttype')
    ->execute();
  $nodes = node_load_multiple(array_keys($result['node']));

++Andy
Developing Drupal websites for Livelink New Media since 2008

DrCord’s picture

This is very useful, thanks! Drop this snippet in and find any content type! Easy, done.

JurgenR’s picture

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

Jaypan’s picture

Some good benchmarking there. For me it just reinforces what I've always believed, which is why I don't use EFQ.

JurgenR’s picture

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

kingandy’s picture

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

tajdar’s picture

Worked perfectly. Thanks..

tyrpower’s picture

Directly through node_load_multiple();
More info at: http://drupal.stackexchange.com/questions/77798/how-to-iterate-over-all-...

bribread22’s picture

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:

  // Query all of the nids of a particular content type.
  $nids = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('type', 'article', '=')
    ->execute()
    ->fetchCol();
  
  // Get all of the article nodes.
  $nodes = node_load_multiple($nids);
Elin Yordanov’s picture

I think the most compact and fastest way to do it:

$nids = db_query('SELECT nid FROM {node} WHERE type =  :type', array(
    ':type' => 'YOUR_CONTENT_TYPE',
  ))->fetchCol();

Old usernames: pc-wurm, Елин Й.

Anonymous’s picture

$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'yourcontenttype');
$entity_ids = $query->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($entity_ids);

argenisfd’s picture

Hello, I new with Drupal, I am a Symfony user.
Please, can you provide documentatión for this database interaction?
Thank you!! :)

Jaypan’s picture

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.

argenisfd’s picture

Thanks, I will check it!