EFQ query tag to only return nodes without comments

Public

There's no direct way in EFQ to query all the nodes that don't have comments. The below example adds a tag which you can then add to your EFQ statemnt via $query->addTag('nocomments'); which will then only allow nodes that don't have comments to be returned in the result.

Get raw version
php
  1. /**
  2.  * Implements hook_query_TAG_alter().
  3.  * This allows us to relate nodes with comment statistics in an EFQ
  4.  * so that we can pull things that don't have comments
  5.  */
  6. function YOURMODULENAME_query_nocomments_alter(QueryAlterableInterface $query) {
  7. $query->join('node_comment_statistics', 'ncs', 'ncs.nid = node.nid');
  8. $query->fields('ncs', array('comment_count'));
  9. $query->condition('ncs.comment_count', 0, '=');
  10. }