EntityFieldQuery Example

Public

A full-rounded EntityFieldQuery example. @see: http://drupal.org/node/1343708

</> CopyGet raw version
php
  1. $query = new EntityFieldQuery();
  2.  
  3. $query->entityCondition('entity_type', 'node')
  4. ->entityCondition('bundle', 'article')
  5. ->propertyCondition('status', 1)
  6. ->fieldCondition('field_news_types', 'value', 'spotlight', '=')
  7. ->fieldCondition('field_photo', 'fid', 'NULL', '!=')
  8. ->fieldCondition('field_faculty_tag', 'tid', $value)
  9. ->fieldCondition('field_news_publishdate', 'value', $year. '%', 'like')
  10. ->range(0, 10)
  11. ->addMetaData('account', user_load(1)); // Run the query as user 1.
  12.  
  13. $result = $query->execute();
  14.  
  15. if (isset($result['node'])) {
  16. $nids = array_keys($result['node']);
  17. $nodes = entity_load('node', $nids);
  18. }

Comments

jcudmore's picture

Cool, I could have used this a few days ago!

One thing that might help people is if you need to filter by the title field you can do it like so:

  1. ->propertyCondition('title', '%'.$value.'%', 'LIKE');