37

Is there a Drupal API function like node_load() that will return a list of nodes by a given type?

I've tried $nodes = node_load(array("type" => 'student_vote')), but it only returns one node.

I know I could code up something like node_load(), but I wanted to see if there might be anything like that out there already.

47

Depending on the version of Drupal :

drupal 6:

$nodes = db_query('SELECT nid FROM {node} WHERE type="%s"', $type);

drupal 7:

$nodes = node_load_multiple(array(), array('type' => $type));

drupal 8:

$nids = \Drupal::entityQuery('node')
  ->condition('type', 'NODETYPE')
  ->execute();
$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadMultiple($nids);
| improve this answer | |
  • Yes, this would work. – Muneer Jun 27 '11 at 5:45
  • 6
    Bear in mind that the $conditions array is depreciated and will be removed in Drupal 8, for future compatibility it would be better to use EntityFieldQuery (api.drupal.org/api/drupal/includes%21entity.inc/class/…) to get the node ids, then pass those into the first argument of node_load_multiple(). There is a good tutorial here: drupal.org/node/1343708 – Bala Clark Aug 22 '12 at 14:06
  • 1
    Just for clarity, EntityFieldQuery() is no more in Drupal 8. – Elijah Lynn Sep 20 '16 at 21:52
  • D8: if you're writing some sort of batch task and you see 0 results for content with restricted permissions, you might need ->accessCheck(FALSE) – William Turrell Dec 31 '19 at 14:18
13

There is no such API for Drupal 6. The closest you can do is to properly query for all node ID for the content type then load each one using node_load() but this will require n+1 queries and is not very efficient.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $nodes = array();
  $query = db_rewrite_sql("SELECT nid FROM {node} n WHERE type = '%s'", 'n');
  $results = db_query_range($query, $type, $offset, $limit);
  while($nid = db_result($results)) {
    $nodes[] = node_load($nid);
  }
  return $nodes;
}

Note: db_rewrite_sql will ensure access check and other module provided filtering (such as language filtering provided by the i18n module).

For Drupal 7, you can use $nodes = node_load_multiple(array(), array('type' => $type));but the $conditions argument of node_load_multiple() is deprecated. Instead, you should use EntityFieldQuery to query for the node IDs then use node_load_multiple() but without a $conditions argument.

function node_load_by_type($type, $limit = 15, $offset = 0) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $type)
    ->range($offset, $limit);
  $results = $query->execute();
  return node_load_multiple(array_keys($results['node']));
}
| improve this answer | |
  • Actually, if you do a node_load() in D6 like this: node_load( array( 'type' => 'page' ) ) you will get all an array of all page nodes. – Blake Senftner Jun 27 '11 at 14:27
  • @bsenftner node_load( array( 'type' => 'page' ) ) only returns one node. – chim Sep 25 '12 at 10:48
7

There's several good answers already, but they take the question literally and refer to nodes only.

Since D6 doesn't have an API for doing what is being asked for, and it's not necessary to limit oneself to nodes in D7 and forward, I feel a good answer should be entity generic.

function entity_load_by_type($entity_type, $bundle, $limit = 10, $offset = 0) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', $entity_type)
    ->entityCondition('bundle', $bundle)
    ->range($offset, $limit);
  $results = $query->execute();
  return entity_load($entity_type, array_keys($results[$]));
}
| improve this answer | |
  • I was going to add an answer about EntityFieldQuery, but you have already written yours. I would just add that the second argument of user_load_multiple() has been deprecated since Drupal 7, and the used code should be the one you show. – kiamlaluno Jul 26 '13 at 11:53
  • I figured I didn't even need to refer to the old versions with deprecated arguments since I just used entity_load right away. That way there's "no" risk of people accidentally ending up using the deprecated functionality. Or do you disagree with that? – Letharion Jul 26 '13 at 11:58
  • 1
    did you mean array_keys($results[$entity_type]) ? – commonpike Feb 5 '15 at 13:21
  • @commonpike Most likely, yes. I don't have the opportunity to check at the moment, but feel free to edit and correct if you test it out. :) – Letharion Feb 7 '15 at 14:18
  • I was doing something slightly different and needed to specify entity_load($entity_type, array_keys($results['node']));. Havent tested it for other entities.. – commonpike Feb 7 '15 at 20:01
1

drupal 8:

$nids = \Drupal::entityQuery('node')
  ->condition('type', 'student_vote')
  ->execute();
$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadMultiple($nids);
| improve this answer | |
1

Get the list of nodes from a content type

Drupal 6:

$nodes = db_query('SELECT nid FROM {node} WHERE type="%s"', 'student_vote');

Drupal 7:

$nodes = node_load_multiple(array(), array('type' => 'student_vote'));

Drupal 8:

$nids = \Drupal::entityQuery('node')
  ->condition('type', 'student_vote')
  ->execute();
$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadMultiple($nids);

Hope that this would be helpful.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.