rsanchez / json

Output ExpressionEngine data in JSON format.
http://devot-ee.com/add-ons/json/
101 stars 234 forks source link

Filtering Channel Field entries before sending them as JSON #34

Closed vlatkorun closed 10 years ago

vlatkorun commented 10 years ago

Is there some way to filter the entries based on the values in the custom channel fileds before sending them as JSON response?

Here is what I need: <?php if($_GET['start'] && $_GET['end']) : ?> //Include only the Events that have event_start_date >= start && event_end_date <= end {exp:json:entries channel="events" xhr="yes" terminate="yes" fields="title|url_title|event_start_date|event_end_date|event_introtext|event_image"} <?php endif; ?>

rsanchez commented 10 years ago

If you are on EE 2.7+, you can use the search parameter's numeric matching for this, assuming start and end are unix timestamps. http://ellislab.com/expressionengine/user-guide/add-ons/channel/channel_entries.html#numeric-matching

<?php if (ee()->input->get('start') && ee()->input->get('end')) : ?>
{exp:json:entries
    channel="events"
    xhr="yes"
    terminate="yes"
    fields="title|url_title|event_start_date|event_end_date|event_introtext|event_image"
    search:event_start_date=">=<?php echo (int) ee()->input->get('start'); ?>"
    search:event_end_date="<=<?php echo (int) ee()->input->get('end'); ?>"
}
<?php endif; ?>
vlatkorun commented 10 years ago

Is it possible to filter the Entries based on a Relationships field, before sending them?

rsanchez commented 10 years ago

No, unfortunately EE does not offer a native way to filter entries based on a relationship field. I believe you can if you used Playa instead of the native relationships field.

vlatkorun commented 10 years ago

I've tried like this, but it doesn't work: {exp:json:entries channel="art" xhr="yes" terminate="yes" show_categories="yes" {exp:playa:coparents field="artist_relation" child_id="79"} {/exp:playa:coparents}}

Is the syntax wrong?

rsanchez commented 10 years ago

I think what you need is the coparent_ids tag, and then feed that into the json plugin's entry_id parameter. You'll probably have to stick it in an embed to get parse order to work.

{embed="site/json_art" coparent_ids='{exp:playa:coparent_ids field="artist_relation" child_id="79"}'}
{exp:json:entries channel="art" xhr="yes" terminate="yes" show_categories="yes" entry_id="{embed:coparent_ids}"}

There are other tags in Playa that output a list of entry_ids: parent_ids, child_ids, sibling_ids. I do not know the particulars of your relationships setup, but hopefully this sets you down the right path.