Skip to content

Instantly share code, notes, and snippets.

@objectivehtml
Created August 20, 2012 20:27
Show Gist options
  • Select an option

  • Save objectivehtml/3407563 to your computer and use it in GitHub Desktop.

Select an option

Save objectivehtml/3407563 to your computer and use it in GitHub Desktop.
New Channel Data Example
<?php
/*
Advanced Query with Channel Data
Obviously the logic in this query is not something that anyone
with any sense would actually use. Rather, this example simply
shows the order of operations and everything that is possible
with the new version of Channel Data (v.8.0).
*/
$entries = $this->EE->channel_data->get_channel_entries($channel_id = 13, array(
'where' => array(
'channel_titles.entry_id !=' => array(
'1',
'OR 2',
'OR 4'
),
'OR channel_titles.title' => 'Some Value Here',
'{1} OR channel_titles.title' => 'Some Other Value',
'{2} OR channel_titles.title' => 'Yet Another Value',
'OR channel_titles.entry_id >' => 100,
'channel_titles.entry_id <' => 150,
'{2} channel_titles.entry_id !=' => 125
)
));
// The above code would generate the following query.
// (Note, it does not return the query, this is query being executed)
/*
SELECT
`exp_channel_titles`.`entry_id`,
`exp_channel_titles`.`channel_id`,
`exp_channel_titles`.`title`,
`exp_channel_titles`.`author_id`,
`exp_channel_titles`.`entry_date`,
`exp_channel_titles`.`expiration_date`,
`exp_channel_titles`.`status`,
`exp_channel_data`.`field_id_142` as 'member_marker_json',
`exp_channel_data`.`field_id_141` as 'member_longitude',
`exp_channel_data`.`field_id_140` as 'member_latitude',
`exp_channel_data`.`field_id_134` as 'member_company',
`exp_channel_data`.`field_id_133` as 'member_twitter',
`exp_channel_data`.`field_id_132` as 'member_phone_number',
`exp_channel_data`.`field_id_131` as 'member_country',
`exp_channel_data`.`field_id_130` as 'member_postal_code',
`exp_channel_data`.`field_id_129` as 'member_state', `exp_channel_data`.`field_id_128` as 'member_city', `exp_channel_data`.`field_id_127` as 'member_address_2',
`exp_channel_data`.`field_id_126` as 'member_address_1',
`exp_channel_data`.`field_id_125` as 'member_last_name',
`exp_channel_data`.`field_id_124` as 'member_first_name'
FROM
(`exp_channel_titles`)
JOIN
`exp_channel_data` ON `exp_channel_titles`.`entry_id` = `exp_channel_data`.`entry_id`
WHERE
(`exp_channel_data`.`channel_id` = 13) AND
(`exp_channel_titles`.`entry_id` != '1' OR `exp_channel_titles`.`entry_id` != '2' OR `exp_channel_titles`.`entry_id` != '4') OR
(`exp_channel_titles`.`title` = 'Some Value Here') OR
(`exp_channel_titles`.`title` = 'Some Other Value') OR
(`exp_channel_titles`.`title` = 'Yet Another Value') OR
(`exp_channel_titles`.`entry_id` > 100) AND
(`exp_channel_titles`.`entry_id` < 150) AND
(`exp_channel_titles`.`entry_id` != 125)
ORDER BY
`exp_channel_titles`.`channel_id` DESC
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment