7 entity.inc public EntityFieldQuery::fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL)

Adds a condition on field values.

Note that entities with empty field values will be excluded from the EntityFieldQuery results when using this method.

Parameters

$field: Either a field name or a field array.

$column: The column that should hold the value to be matched.

$value: The value to test the column value against.

$operator: The operator to be used to test the given value.

$delta_group: An arbitrary identifier: conditions in the same group must have the same $delta_group.

$language_group: An arbitrary identifier: conditions in the same group must have the same $language_group.

Return value

EntityFieldQuery The called object.

See also

EntityFieldQuery::addFieldCondition

EntityFieldQuery::deleted

File

includes/entity.inc, line 675

Class

EntityFieldQuery
Retrieves entities matching a given set of conditions.

Code

public function fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
  return $this->addFieldCondition($this->fieldConditions, $field, $column, $value, $operator, $delta_group, $language_group);
}

Comments

The $delta_group parameter allows you to group conditions together. I have always read the docs to mean that we were specifying the delta within the field itself, but this is not the case.

For example, if you want to select entities based on two columns from the same field, you use a delta group to make sure that the conditions are dependent.

For example, to select users for an organic group with a specific state, I have to add the $delta_group (0) to the fieldCondition. Without it there is nothing to connect the group and the state.

$query
    ->entityCondition('entity_type', 'user')
    ->fieldCondition('group_audience', 'gid', $gid, '=', 0)
     ->fieldCondition('group_audience', 'state', (array) $states, 'IN', 0);

I had a little trouble finding the right $column to use, here is a little breakdown of what I learned, hope it helps!

  1. When you create the field entitled field_yourfield drupal creates the mysql table field_data_field_yourfield
  2. Depending on what type of field you created, will see different information stored in this table but this table's column is what you will feed in for $column
    NOTE: the name of your field might preface some of the columns such as "field_yourfield_var" to use this column simply enter "var"

Hope this helps future googlers

Just to make it more clear I'll put an example.
If you have a field named 'field_city', the table will be 'field_data_field_city'.
In this table will be several columns. As column you have to write the name of the column as is in the db, except for those that begin by the name of the field. If you have a column named 'field_city_value' you just need to write 'value'. If you have a column named 'deleted' you have to write 'deleted'.

Example:

$result = $query
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_city', 'value', 'Barcelona', '=')
  ->execute();

While fields such as text use '_value' other times you are going to want to check the field's schema for specific column name.

To do taxonomy term reference lookup using EntityFieldQuery you will want to reference the column field_TITLE_tid as 'tid'. In the example below, the taxonomy term reference field I am using is called 'foo' and I am querying the table 'field_data_field_foo' for the column 'filed_foo_tid'.

<?php
  $query
= new EntityFieldQuery();
 
$query
   
->entityCondition('entity_type', 'node')
    ->
entityCondition('bundle', 'article')
    ->
propertyCondition('status', 1)
    ->
propertyOrderBy('created', 'DESC')
    ->
fieldCondition('field_foo', 'tid', array('1', '2'))
    ->
range(0,10);
 
$result = $query->execute();
?>

You can also find what 'column' to use by looking at field_info_fields() command, which produces a keyed array: keys being the field_name, and value being the properties of that field. Looking at the property, you will see another key called 'columns'. I observe that the first value is usually what the 'column' to use, which is usually 'value', but for different field types it could be different -- such as tid for taxonomy, fid for images and files, etc.

It would be helpful to dig in the code of field api to see how drupal determines exactly which column to use.

These worked for me:

->fieldCondition('field_name', 'value', 'NULL', '!=');

and use this for nid reference:

->fieldCondition('field_name_ref', 'nid', 'NULL', '!=');

I had to go this way to avoid errors on some plateforms
->fieldCondition('field_name', 'value', NULL, 'IS NOT NULL');

hi
Any one help me i am using the between on query but it's not working
see this code.
$query->fieldCondition('field_amountpaid', 'value', array('501','5000'),'BETWEEN');

I'm using the Entity reference module and want to select all nodes that has no parent defined. I'm trying with:

$query = new EntityFieldQuery();
$children = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'page')
->propertyCondition('status', 1)
->fieldCondition('field_parent', 'target_id', null, 'IS NULL')
->execute();

.. but that does not give anything as the field "field_parent" is missing and not set to NULL. If I use IS NOT NULL I get all nodes that has a parent.

How can I select all nodes that has no parent (or is missing the field "field_parent")?

Helpfully documented with the internal function at EntityFieldQuery::addFieldCondition, I duplicate them here for our convenience:

$operator:
Possible values:

  • '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
    operators expect $value to be a literal of the same type as the
    column.
  • 'IN', 'NOT IN': These operators expect $value to be an array of
    literals of the same type as the column.
  • 'BETWEEN': This operator expects $value to be an array of two literals
    of the same type as the column.

The operator can be omitted, and will default to 'IN' if the value is an
array, or to '=' otherwise.

Helpful article with example and explanations at How to Use EntityFieldQuery https://www.drupal.org/node/1343708

how to apply fieldCondition on multivalue entitytreference ie target_id?
tested fieldCondition('field_er_multi', 'target_id', array(1), 'IN') but no success.