$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'MYCONTENTTYPE')

// Now say for instance I would like to grab the value of
// field_A and field_B, sum them up ( or subtract | divide | multiply them ).
// Then compare the result of that ( is greater than, equals to, smaller than )
// with a given number X. Is it possible with EntityFieldQuery?

$query_result = $query->execute();

I know I could do that by looping through the query result. Nontheless, if EntityFieldQuery allows me to do that at query time, I would like to know how.

1 Answer 1


I dont believe you can do this with the core methods provided by the EFQ api.

But, you may always extend the default EFQ class with a version of your own. Or, you may implement a function such as hook_entity_query_alter() or the addTag() method of EFQ.

Using addTag for instance on the EFQ you could then also implement hook_query_TAG_alter() and alter the SelectQuery that is ultimately called from the original EFQ and alter the base query to include your additional (non-standard) logic.

This could look like this short example, we additionally use the SelectQuery::where() method:

// Within some function of yours, you execute your query; giving it a Tag.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'MYCONTENTTYPE')
  // Must call the method fieldCondition on field_A and field_B so
  // they get included in the SQL INNER JOIN statement.
$query_result = $query->execute();

// ... elsewhere in your custom module ...

// Implements hook_query_TAG_alter
function mymodule_query_DoFunkyFieldLogic_alter(QueryAlterableInterface $query) {
  if ($query->hasTag('DoFunkyFieldLogic')) {
    // You will likely need to install Devel and call dpm($query) here.
    // Or else you wont know what the field names are in the raw SQL ...
    // you might be able to cheat and use full database table names and
    // column names, eg:
    $query->where("field_data_field_A1.field_A_value + field_data_field_B2.field_B_value <= :my_quantity", array(':my_quantity' => variable_get('my_quantity', 5));

There may be cleaner ways to do this -- but this approach is relatively straight forward. This code is untested; but should work.

  • Awesome! your solution does the trick, thanks. I had to do a minor correction though. Jun 18, 2013 at 6:10
  • @FranciscoLuz whats the correction so i can amend the post ? (or feel free to Edit the post as well) ...
    – tenken
    Jun 18, 2013 at 6:25
  • Weird. I've edited your answer but it's not there anymore. Here it is, bit.ly/13WOp4O Jun 18, 2013 at 6:32
  • 1
    ah i see, i approved the edit. Yes including the fieldCondition will add the join for your ... it's basically your choice (and probably wise) to include it that way for later processing. You could always just addJoin on the SelectQuery itself ... good catch.
    – tenken
    Jun 18, 2013 at 6:49
  • 1
    Ya ! It's very cool. We can use the same way for db_select, db_update, db_select.
    – Stone Vo
    Jun 18, 2013 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.