I’d like to programmatically get channel entries from within a plugin, outside of the template in EE 2. The channel api seems to only handle create/update/delete operations, but I’d like to retrieve entries based on parameters. I found some code for EE 1.6 using the Weblog module on slide 70 of a solspace presentation:http://www.slideshare.net/reedmaniac/addon-development-ee-expects-that-every-developer-will-do-his-duty-2494809?src=related_normal&rel=4412482. Is that code applicable to the Channel class in EE 2? Is there another way?
Take a look at the model class ‘channel_entries_model’, you can find it in expressionengine/models/channel_entries_model.php.
It has functions like get_entry( entry_id, channel_id, autosave ), which I think will fit the bill for what you need.
You can call it like this:
$query_result = $this->EE->channel_entries_model->get_entry( $entry_id, $channel_id );
if( $query_result->num_rows() > 0 )
{
foreach( $query_result->result() as $entry )
{
// etc, etc
}
}To understand query result hydration better and know what your options are, check out the CodeIgniter Documentation.
If you’re more interested in retrieving an entry or list of entries based on some parameters (and it looks like this might be your case), you can use a plugin and a custom tag to wrap the channel:entries tag (so you can call exp:my_plugin:entries, which in turn calls exp:channel:entries after doing some custom processing / modifications ).
It’s as simple as this:
/**
* Wraps the channel:entries tag and provides additional
* functionality.
*/
function entries()
{
// use code like this to grab passed tag parameters
$time_window = $this->EE->TMPL->fetch_param( 'time_window' ) );
// use code like this to set tag parameters for channel:entries to see
$this->EE->TMPL->tagparams[ 'status' ] = 'closed';
// now it's time to call channel->entries() to do the heavy lifting
if( !class_exists( 'channel' ) )
require_once PATH_MOD . "channel/mod.channel" . EXT;
$class = new Channel();
return $class->entries();
}I found the channel_entries_model->get_entry() function, but what I dont understand is why you need to pass it a channel id, if entry id’s are unique.
So to get an entries data you have to call the function twice, first to get the entries channel ID and then to get the entry data.
I guess you could write your own select statement, but I like using existing functions, does anyone know why this function is written like this?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.