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:
fieldCondition between is not working on drupal 7 any idea.
LikeLike
Never try the before. let me spend some time on it and update you later.
LikeLike
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.
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
Kit
LikeLiked by 1 person
Thank you this was very useful
LikeLike
You are welcome. =)
LikeLike
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().”
LikeLike