bradrice
bradrice

Reputation: 1755

Drupal 7 EntityFieldQuery using two fields

I have created a pull of images based upon a boolean field being checked. Now I would like to expand that conditional logic to also select from categories, using either a select list or a taxonomy list. Here is what I have:

function ycs_list() {
  $query = new EntityFieldQuery;
  $query->entityCondition('entity_type', 'node')
 ->entityCondition('bundle', YCS_NODE_TYPE)
  ->propertyCondition('status', 1)
  ->fieldCondition('field_active_image', 'value', 1, '=');
  $result = $query->execute();
  return $result;
}

How can I expand this to use two fileds? If I were to use a taxonomy list, would I use something other than a EntityFieldQuery or is it just an additional condition of this?

Upvotes: 2

Views: 3804

Answers (1)

Clive
Clive

Reputation: 36957

You can add as many conditions to an EntityFieldQuery as you like and as vocabularies are linked to nodes via fields in Drupal 7 you can include them quite easily. The only small thing to note is that the column name for a term ID in a term reference field is tid instead of value which is used in a lot of cases.

The following assumes your term reference field is named field_my_term_field:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', YCS_NODE_TYPE)
  ->propertyCondition('status', 1)
  ->fieldCondition('field_active_image', 'value', 1, '=')
  ->fieldCondition('field_my_term_field', 'tid', $the_term_id);

$result = $query->execute();
return $result;

Upvotes: 1

Related Questions