Using Drupal 7 I have created a content type called 'fauna'. I simply want to filter by an attribute (eg: 'area') using Drupal database API.

As far as I have seen around, most of people follow queries like...

$query = db_select('my_table_name')

applying then some filters.

But I have no table at all regarding my content type where I could apply these filters, i just have a bunch of tables field_data_field_fauna_name, field_data_field_fauna_area ... (these are the fields defined for my Content type).

  • U have to filter by an attribute area value ah? – Nandhana Jul 31 '14 at 12:42

Instead of querying directly against a table you can use Entity field query.

I'll assume the machine name of your content type is called fauna and that there is a field called field_fauna_area. Now lets find all nodes where the field field_fauna_area is "Europe".

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'fauna')
  ->fieldCondition('field_fauna_area', 'value', 'Europe', '=')
$result = $query->execute();
if (isset($result['node'])) {
  $fauna_nids = array_keys($result['node']);
  $fauna_items = entity_load('node', $fauna_nids);

In $fauna_nids you will find all node ids and in $fauna_items all the nodes will be loaded.


You want to look at the node table where all nodes of all content types are listed. Then filter by your content type there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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