Is there a function in Drupal 7 to pull all instances of a specific content type to store in an array? Ex: Say I have a content type called "us_states" and I want to pull each instance of that type (with it's saved data so the title, description, and the custom content I created) and store them into an array. How should I go about doing that?
$nodes = node_load_multiple(array(), array('type' => 'us_states'));
The second argument (
$conditions) is marked as deprecated; and you can see that in the Drupal 8 documentation for the same function, it has been removed. I only included this example because I'm sure someone else would have pointed it out had I not and the fact that you specifically asked for a function.
$query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'us_states'); $nodes = $query->execute();
In both examples,
$nodes will be an array containing all nodes of the content type 'us_states'. (Note that the nodes within the array will be objects per usual.)
If you really need to use a function (that won't be deprecated in Drupal 8), you can pass an array of Node IDs as the first param to
node_load_multiple() that contains the Node IDs of all of the 'us_states'.
You need to get an array of node ids from the database first. Simple
db_select query will do the job.
$nids = db_select('node') ->fields('node', array('nid')) ->condition('node.type', 'us_states') ->execute() ->fetchCol();
And then pass that array to
You should be OK memory-wise when loading 50 nodes all at once. But you should be cautious when the total number of nodes is significant (more than 100 I'd say).