9

I have Vocabulary and Custom Content Type which has field Term reference.

How can I get all nodes of particular term in my module?

Something like this? This doesn't work.

$field = field_info_field('field_game');
$results = new EntityFieldQuery;
$results->fieldCondition($field, 'field_game_tid', '5')
  ->execute();

$nids = array();
foreach($results as $result) {
  $nids[] = $result->nid;
}

$nodes = node_load_multiple($nids);
13

You want to specify that you searching for nodes first (and optionally by your content type), and then filter by term:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'YOUR_CONTENT_TYPE_HERE')
->fieldCondition('field_game', 'tid', 5);

$result = $query->execute();
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);

Here's a good example of an entity query like this (and more field conditions)

| improve this answer | |
4

There is an API function taxonomy_select_nodes() for that:

$tid = 5;
$nids = taxonomy_select_nodes($tid, FALSE); // FALSE for paging.
$nodes = node_load_multiple($nids);
| 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.