I've found node_load_multiple() which allows me to get all the nodes, but I need to specify some parameters, e.g. the type of node that I'd like to fetch.
I'd like to get all the nodes in the system without the criteria. How can I do that?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI've found node_load_multiple() which allows me to get all the nodes, but I need to specify some parameters, e.g. the type of node that I'd like to fetch.
I'd like to get all the nodes in the system without the criteria. How can I do that?
The easiest way would be to use entity_load()
$nodes = entity_load('node');
If you don't provide the second argument ($ids
) it will load up all entities of the given type:
$ids: An array of entity IDs, or FALSE to load all entities.
Just for the sake of completeness node_load_multiple()
doesn't require the $conditions
parameter so you could also grab all of the node ids from the database
$nids = db_query('SELECT nid FROM {node}')->fetchCol();
And then use $nids
to load the nodes:
$nodes = node_load_multiple($nids);
Just use node_load_multiple(FALSE)
, and you will get all the nodes present in the site.
Keep in mind that it returns all the nodes, and this would be a problem when there are many nodes: The function takes time to be executed, and each node uses memory to be stored. With enough nodes, you would exhaust all the available memory.
Using the EntityFieldQuery class, you can limit the number of returned nodes.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
// It returns the first 10 nodes.
// Use range(10, 10) to get the next 10 nodes.
->range(0, 10);
$result = $query->execute();
if (isset($result['node'])) {
$nodes = entity_load('node', array_keys($result['node']));
}
I added propertyCondition('status', 1)
because you are normally interested on published nodes; if you want to get all the nodes, remove that line.
Notice that the $conditions
parameter for node_load_multiple() is not anymore used in Drupal 8. If you need to get some nodes basing on some conditions, you should use the EntityFieldQuery
class.
Here is another way to find all node ids in your website.
$nids = array_values(\Drupal::entityQuery('node')->execute());