Query nodes on location cck data using EntityFieldQuery

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

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: