php - WP REST API query multiple post types -
i trying retrieve posts multiple post types using wp rest api. have no problem getting feed 1 post type book
doing this:
http://example.com/wp-json/posts?type=book&filter[posts_per_page]=10
now want extend feed book
, movie
. gives me last specified type:
http://example.com/wp-json/posts?type=book&type=movie&filter[posts_per_page]=10
this gives me error:
http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10
how should handling this?
thanks!
edit: fixed syntax match have. here errors when syntax http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10
used:
warning: urlencode() expects parameter 1 string, array given in /home/newbreak/public_html/wp-includes/formatting.php on line 4128
warning: cannot modify header information - headers sent (output started @ /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587
warning: cannot modify header information - headers sent (output started @ /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587
warning: cannot modify header information - headers sent (output started @ /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587
warning: cannot modify header information - headers sent (output started @ /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587
warning: cannot modify header information - headers sent (output started @ /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587
warning: cannot modify header information - headers sent (output started @ /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587
i errors when send type array type[]
.
you can try registering additional endpoint don't have add many query arguments in each request. i'm using latest version of wp rest api plugin.
so custom content type called "movies", have endpoint wp-json/wp/v2/movies
add_action('rest_api_init', 'register_movies'); function register_movies() { $movies_custom_fields = array( 'director', 'genre' ); foreach ($movies_custom_fields $key) { register_rest_field('movies', $key, array( 'schema' => null, 'get_callback' => 'get_movies_data', )); } } function get_movies_data( $object, $field_name, $request ) { return get_post_meta( $object[ 'id' ], $field_name, true ); }
more documentation on adding custom endpoints different content types @ wp rest api v2 documentation site.
Comments
Post a Comment