Drupal 7 – Get specific nodes using EntityFieldQuery

Starting from Drupal 7, you can now select nodes with conditions using the EntityFieldQuery class. The following are a few examples.

Select nodes of a specific content type written by a specific author and posted 1 hour ago.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', '<CONTENT TYPE>') // ex. article
  ->propertyCondition('uid', $uid) // node written by a specific user
  ->propertyCondition('status', 1) // published nodes
  ->propertyCondition('created', REQUEST_TIME - 3600, '>='); // created within the past 1 hour
$result = $query->execute();

/*
print_r($result)
================
Array
(
  [node] => Array
  (
    [529] => stdClass Object
    (
      [nid] => 529
      [vid] => 529
      [type] => <CONTENT TYPE>
    )

    [531] => stdClass Object
    (
      [nid] => 531
      [vid] => 531
      [type] => <CONTENT TYPE>
    )

  )
)
*/

 

Select nodes of a specific content type with a field_name = ‘eureka’.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', '<CONTENT TYPE>') // ex. article
  ->propertyCondition('status', 1) // published nodes
  ->fieldCondition('field_name', 'value', 'eureka', '='); // field_name = 'eureka'
$result = $query->execute();

 

Select nodes of a specific content type with user reference field associated to a user with uid = $uid

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', '<CONTENT TYPE>') // ex. article
  ->propertyCondition('status', 1) // published nodes
  ->fieldCondition('field_user_ref', 'uid', $uid, '='); 
$result = $query->execute();

 

Usually if the conditions are the columns of the node table, use propertyCondition(). For other fields, use fieldCondition().

Done =)

Next: Drupal 7 – Get mulitple nodes using EntityFieldQuery and node_load_multiple()

Reference:

Advertisement

8 thoughts on “Drupal 7 – Get specific nodes using EntityFieldQuery”

    1. Hi keshav,

      i couldn’t find any reference about ‘BETWEEN’ does not work in fieldCondition. i read your comment @ Drupal API – EntityFieldQuery and the following is your fieldCondition setting.

      $query->fieldCondition('field_amountpaid', 'value', array('501','5000'),'BETWEEN');
      

       

      i think the above query should work. but could you check the db table with name = field_data_field_amountpaid and give me the name and the some values of the last column? it should be named as field_amountpaid_XXX.

      And i think you can try giving the value without the quote such that

      $query->fieldCondition('field_amountpaid', 'value', array(501,5000),'BETWEEN');
      

      Kit

      Liked by 1 person

  1. The following line was the key piece of info on this page, in my opinion. Thanks!

    “Usually if the conditions are the columns of the node table, use propertyCondition(). For other fields, use fieldCondition().”

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.