Query nodes on location cck data using EntityFieldQuery

Last updated on
30 April 2025

If you want to select nodes based on location data stored in a location_cck field you can do so like this (replace NODETYPE & FIELDNAME with your required node type and field name):

This example returns nodes that have a location with province = $province

<?php
$subquery = db_select('location');
$subquery->addField('location', 'lid');
$subquery->condition('location.province', $province);

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'NODETYPE')
  ->fieldCondition('FIELDNAME', 'lid', $subquery, 'IN');

$result = $query->execute();
?>

If you want full entities instead of just the stubs, add this after:

<?php
if (!empty($result['node'])) {
  $entities = entity_load('node', array_keys($result['node']));
}
?>

Help improve this page

Page status: Not set

You can: