Skip to content

Instantly share code, notes, and snippets.

@tomothumb
Created October 2, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomothumb/8d9005aa77f72706d578 to your computer and use it in GitHub Desktop.
Save tomothumb/8d9005aa77f72706d578 to your computer and use it in GitHub Desktop.
api request with filtering by custom field for Wordpress REST API v1 see: https://github.com/WP-API/WP-API/issues/1060
add_filter( 'json_query_vars', 'slug_allow_meta' );
function slug_allow_meta( $valid_vars ) {
$valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) );
return $valid_vars;
}
$.ajax({
type: 'GET',
url: 'http://xxxxxxxxxx/wp-json/posts?filter[posts_per_page]=20&type[]=page&filter[orderby]=rand&filter[meta_key]=XXXXX&filter[meta_value]=YYYYY',
dataType: 'json'
}).done(function(json){
console.log(json);
}).fail(function(json){
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment