Can I filter entityFieldQuery results by Workbench moderation state? I have the Workbench module and I want to look up nodes which have a specific workbench state with EntityFieldQuery.

I can get articles assigned to a user as follows:

$articles = new EntityFieldQuery();
$articles-&gt;entityCondition('entity_type', 'node')
         -&gt;fieldCondition('field_moderating_user', 'uid', $user_id, '=');


Now I want to add a condition that only gets articles that have the state 'assigned'.

Any ideas?
An EntityFieldQuery can only filter based on fields associated with the entity by default, and since workbench moderation records are held in a custom table outside of the field system the standard query can't include a reference to them.

However, you can extend the query using tags and hook_query_alter, e.g.:

function MYMODULE_query_alter(QueryAlterableInterface $query) {
  // Only change the query if we have the required tag
  if ($query-&gt;hasTag('WorkbenchModerationAssigned')) {
    // Join the workbench moderation node history table
    $query-&gt;join('workbench_moderation_node_history', 'wmnh', 'wmnh.nid = n.nid');

    // Add the state and current conditions
    $query-&gt;condition('wmnh.state', 'assigned');
    $query-&gt;condition('wmnh.current', 1);
  }
}


You would then build up your EntityFieldQuery like this:

$articles = new EntityFieldQuery();
$articles-&gt;entityCondition('entity_type', 'node')
  -&gt;fieldCondition('field_moderating_user', 'uid', $user_id, '=')
  -&gt;addTag('WorkbenchModerationAssigned');


That's a very basic example which may need some changing, you might also want to join in the workbench_moderation_states table but it depends on your exact requirements.

EDIT

Just for a bit more info you can also use hook_query_TAG_alter() to do this in a more targeted manner:

function MYMODULE_query_WorkbenchModerationAssigned_alter(QueryAlterableInterface $query) {
  $query-&gt;join('workbench_moderation_node_history', 'wmnh', 'wmnh.nid = n.nid');
  // ...