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:

Advertisements

7 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

      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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s