sqldrupaldrupal-7entityreference

Drupal - Query to count all nodes of content_type "A" in which field_b is "X"


I need to make a query for a drupal site. Initially I just needed to COUNT the number of nodes for a content type:

$query = "SELECT COUNT(*) amount FROM {node} n WHERE n.type ='A'";
$result = db_query($query)->fetch();
return $result->amount;

Now I need that but only for nodes that have the field_b equal to 'X'. How can I do this?

I tried EntityFieldQuery without sucess:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'A')
  ->fieldCondition('field_b', 'value', 'X');

$results = $query->execute();
return $results->amount;

Any help?


Solution

  • You can set the query to be a count query only by using :

    $count = $query->count()->execute();