0

I have drupal 7 site and I'm trying to listing all articles (with title) and where category field is empty (taxonomy). I'm running this code in my page with ->isNull and without, but not any results. Can someone explain what I'm doing wrong in here and why this is not working?

$query = db_select('node', 'n')
  ->fields('n', array('nid'))
  ->condition('n.type', 'article') 
  ->condition('n.status', 1)
  ->isNull('n.field_tags')
  ->range(0, 10);

  $results = $query->execute();

I'm get this error all the time:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'n.field_tags' in 'where clause': SELECT n.nid AS nid FROM {node} n WHERE (n.type = :db_condition_placeholder_0) AND (n.status = :db_condition_placeholder_1) AND (n.field_tags IS NULL ) LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] => article [:db_condition_placeholder_1] => 1 ) in eval() (line 17 of /sites/site.com/modules/php/php.module(80) : eval()'d code).

  • Welcome to Drupal Answers! 👋And ->condition('n.field_tags', NULL) or ->condition('n.field_tags', [])? Ah yeah, same, column not found... – leymannx Mar 24 at 16:55
  • 3
    Does this answer your question? Excluding empty(Null) fields when using EntityFieldQuery query condition – leymannx Mar 24 at 17:03
  • Just tried it myself and you definitely might go for @Clive's answer over in the linked duplicate issue with EFQ and a custom query tag. Also read through the drupal.org issue they linked in their answer. – leymannx Mar 24 at 18:17
2

It seems to me your mistake is you are requesting the column field_tags from the table node, which is not a column from node, so doesn't exist.
Unless you have a very specific database structure, your "field_tags" should be contained in the table field_data_field_tags, so you may have to make an INNER JOIN between these tables in your query (maybe using field_data_field_tags.entity_id = n.id), and then check if the n.id exists in field_data_field_tags.entity_id, or (maybe better) use a LEFT JOIN between field_data_field_tags and node, so only the node with a n.id existing in field_data_field_tags will be returned.

| improve this answer | |

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.