Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created February 12, 2014 15:18
Show Gist options
  • Save ChrisLTD/8957379 to your computer and use it in GitHub Desktop.
Save ChrisLTD/8957379 to your computer and use it in GitHub Desktop.
Drupal 7 Get nodes by type, taxonomy term, and sort by custom field
<?php
$query = db_select('node', 'n');
$query->join('field_data_field_vertical', 'v', 'v.entity_id=n.nid');
$query->join('field_data_field_date', 'd', 'd.entity_id=n.nid');
$query
->fields('n', array('nid', 'title', 'created'))
->fields('d', array('field_date_value'))
->condition('v.field_vertical_tid', 1)
->condition('n.status', 1)
->condition('n.type', 'event')
->orderBy('d.field_date_value', 'DESC')
/* ->orderBy('created', 'DESC') */
->range(0,3); //LIMIT to 2 records
$result = $query->execute();
while( $record = $result->fetchAssoc() ) {
echo "<pre>" . print_r( $record , true) . "</pre>";
echo url('node/' . $record['nid']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment