37

Is there a function like node_load() that returns a list of nodes by a given content 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.

5 Answers 5

46

The code to use depends from the Drupal version.

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);
4
  • Yes, this would work.
    – Muneer
    Commented Jun 27, 2011 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
    Commented Aug 22, 2012 at 14:06
  • 1
    Just for clarity, EntityFieldQuery() is no more in Drupal 8. Commented Sep 20, 2016 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) Commented Dec 31, 2019 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']));
}
2
  • 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. Commented Jun 27, 2011 at 14:27
  • @bsenftner node_load( array( 'type' => 'page' ) ) only returns one node.
    – chim
    Commented Sep 25, 2012 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[$]));
}
6
  • 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.
    – avpaderno
    Commented Jul 26, 2013 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
    Commented Jul 26, 2013 at 11:58
  • 1
    did you mean array_keys($results[$entity_type]) ?
    – commonpike
    Commented Feb 5, 2015 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
    Commented Feb 7, 2015 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
    Commented Feb 7, 2015 at 20:01
0

Drupal 8+

$content_type = 'article';
$articles = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
  'type' => $content_type,
]);
0

At the end, Drupal 8 deprecated functions like node_load_multiple(), user_load_multiple(), not just the $conditions parameter they used. This means it isn't possible to write Drupal 7 code that is compatible with Drupal 8, as the classes used in Drupal 8 aren't available on Drupal 7.

Drupal 7 code can still use node_load_multiple() and its $conditions parameter. To load all the nodes whose content type is $content_type, it's sufficient to use the following code.

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

A more generic code that works for any entity would be the following one.

$entities = entity_load($entity_type, array(), array('type' => $bundle);

Despite the name, entity_load() loads more entities. It's the function used from node_load_multiple().

function node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('node', $nids, $conditions, $reset);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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