Skip to content

Instantly share code, notes, and snippets.

@rileypaulsen
Created August 23, 2014 23:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileypaulsen/4a46ec4c6241411602a0 to your computer and use it in GitHub Desktop.
Save rileypaulsen/4a46ec4c6241411602a0 to your computer and use it in GitHub Desktop.
Use a tax_query in a JSON REST API request
//requires authentication
add_filter('json_query_vars','accept_new_queries');
add_filter('json_query_var-tax_query', 'filter_by_taxonomy');
function accept_new_queries($filters){
return array_merge($filters,array('tax_query'));
}
function filter_by_taxonomy($val){
return maybe_unserialize(stripslashes($val));
}
//serialize the tax_query array to use in the URL
$tax_arrays = array(
array(
'field'=>'slug',
'taxonomy'=>'designation',
'terms'=>array('staff','student','alumni')
)
)
$url = 'example.com/wp-json/posts?filter[tax_query]='.serialize($tax_arrays)';
@oliviercardoen
Copy link

Thanks! It helped a lot.

As I embedded the tax_query into a data attribute that I grab on the fly, so encoding the tax_query in base64 came very handy : http://davidwalsh.name/php-serialize-unserialize-issues

Cheers,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment