Created
August 23, 2014 23:03
-
-
Save rileypaulsen/4a46ec4c6241411602a0 to your computer and use it in GitHub Desktop.
Use a tax_query in a JSON REST API request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,