Skip to content

Instantly share code, notes, and snippets.

@ivankravchenko
Created September 24, 2013 11:51
Show Gist options
  • Save ivankravchenko/6683621 to your computer and use it in GitHub Desktop.
Save ivankravchenko/6683621 to your computer and use it in GitHub Desktop.
Wordpress JSON-API support for Sencha Ext.Store filters
<?php
// Wordpress JSON-API support for Sencha Ext.Store filters
// https://gist.github.com/krava/6683621
add_action('json_api-core-get_recent_posts', 'wapp_customize_get_recent_posts');
function wapp_customize_get_recent_posts() {
// filter: [{"property":"category","value":"1"},...] -> cat=:id, ...
global $wp_query;
if (isset($_GET['filter'])) {
$filter = json_decode(stripslashes($_GET['filter']));
foreach ($filter as $singleFilter) {
$property = $singleFilter->property;
$value = $singleFilter->value;
switch ($property) {
case 'category':
$wp_query->query['cat'] = $value;
break;
case 'fromDate':
$wp_query->query['date_from'] = $value;
break;
case 'toDate':
$wp_query->query['date_to'] = $value;
break;
default:
die("Unknown filter: $property");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment