What is the best way to do a db query for fields in a content type?

I need to get specific fields from a content type, but only where the 'featured' field value is 1.

        $query = new EntityFieldQuery();

        $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'mycontenttype')
          ->propertyCondition('status', NODE_PUBLISHED)
          ->fieldCondition('field_mypdf', 'value', 'NULL', '!=')
          ->fieldCondition('field_mypdf', 'fid', 'NULL', '!=')
          ->fieldCondition('field_mypdf_image', 'fid', 'NULL', '!=')

Originally, I was doing something along the lines of:

$query = "SELECT node.nid AS nid, 'node' AS field_data_field_news_node_entity_type, 'node' AS field_data_field_news_date_text_node_entity_type, 'node' AS field_data_field_news_teaser_node_entity_type, 'node' AS field_data_field_news_body_node_entity_type, 'node' AS field_data_field_news_video_node_entity_type, 'node' AS field_data_field_news_video_image_node_entity_type, 'node' AS field_data_field_mypdf_node_entity_type
    FROM node
    INNER JOIN field_data_field_news_is_featured ON node.nid = field_data_field_news_is_featured.entity_id AND (field_data_field_news_is_featured.entity_type = 'node' AND field_data_field_news_is_featured.deleted = '0')";

$result = db_query($query);

<?php foreach ($result as $row) : ?>

    <div class="featuredArticle">
        <?php print $row->field_mypdf_value; ?>
        <?php print $row->field_mypdf_fid; ?>

<?php endforeach; ?>
  • Can you not create a view adding featured as a 1 as one of the filter criteria..? Commented Jun 3, 2015 at 20:39
  • Yes, I can. The featured field is actual not the most important part. I just wanted to be sure of the best way to get the fields from content type
    – srodrig
    Commented Jun 3, 2015 at 20:57
  • Views module does this.
    – No Sssweat
    Commented Jun 3, 2015 at 22:15

1 Answer 1


I think choice is in between EntityQuery and db_select and EntityQuery seems to be a better choice for Entities as it takes care of joining different fields for you.

Reference Link: EntityFieldQuery vs Db_select() -

@Bart says: -

I think that the main reason prefering EntityFieldQuery over db_select is that you don't have to know about the lower-level structure, in other words: how stuff is stored in the database. This improves loose coupling.

@Berdir comment:-

This is correct, although it goes even further. The field storage is pluggable. The default implementation stores field data in a separate table per field in the database, but it can be replaced, there is for example an implementation which allows to store field data in MongoDB. If you want your code to be portable and not just work on your specific site configuration, you must use EntityFieldQuery.

  • This is helpful, thanks! I will need to loop through each row of the query ... I can do this wither either method? Also for the EntityFieldQuery, I can add additional fieldCondition? How would I do this just using a SELECT?
    – srodrig
    Commented Jun 4, 2015 at 12:29
  • Also, I need to query all nodes of a given content type, not just a single node.
    – srodrig
    Commented Jun 4, 2015 at 12:37
  • Please check the documentation at drupal.org/node/1343708, best would be to try it out. Your doubts will get clear. With no doubt, Entity Query is traversing through all your nodes. Yes you can add multiple fieldConditions.
    – arpitr
    Commented Jun 4, 2015 at 13:30
  • dropbucket.org/node/54
    – arpitr
    Commented Jun 4, 2015 at 13:40
  • I updated original question with my EntityFieldQuery...what am I doing wrong?
    – srodrig
    Commented Jun 4, 2015 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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