Last active
March 13, 2017 21:13
-
-
Save karelbemelmans/7e5b29258852925367a07f406a24a9b1 to your computer and use it in GitHub Desktop.
Using entity queries in Drupal 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// A. The old way using node_load | |
$nodes = node_load_multiple(NULL, array('type' => 'example_content_type')); | |
// B. The new way using entity queries. | |
// The point of entity queries is you can add field filters here before the | |
// node has to be actually loaded, making your code execution a load lighter. | |
// Get a list of nids for nodes that match our request | |
// Remember that this only return nids, no full node objects! | |
$query = new EntityFieldQuery(); | |
$entities = $query->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', 'example_content_type') | |
->propertyCondition('status', NODE_PUBLISHED) | |
->propertyCondition('language', $language) | |
->fieldCondition('target_access', 'value', $target_access, '=') | |
->execute(); | |
if (count($entities)) { | |
$nids = array_keys($entities['node']); | |
$nodes = node_load_multiple($nids); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment