0

I am trying to retrieve posts from multiple post types using WP REST API. I have no problem getting a feed from one post type book by doing this:

http://example.com/wp-json/posts?type=book&filter[posts_per_page]=10

Now I want to extend the feed to get book and movie. This only gives me the last specified type:

http://example.com/wp-json/posts?type=book&type=movie&filter[posts_per_page]=10

This gives me an error:

http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10

How should I be handling this?

Thanks!

Edit: Fixed syntax to match what I actually have. Here are the errors that I get when this syntax http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10 is used:

Warning: urlencode() expects parameter 1 to be string, array given in /home/newbreak/public_html/wp-includes/formatting.php on line 4128

Warning: Cannot modify header information - headers already sent by (output started at /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 already sent by (output started at /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 already sent by (output started at /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 already sent by (output started at /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 already sent by (output started at /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 already sent by (output started at /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 only get the errors when I send the type as an array type[].

1
  • Have you tried replacing type with post_type? Commented Aug 19, 2015 at 19:51

2 Answers 2

4

You can also try registering an additional endpoint so you don't have to add as many query arguments in each request. I'm using the latest version of the WP REST API plugin.

So for a custom content type called "movies", you could have an endpoint like 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 as $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 for different content types at the WP REST API V2 documentation site.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using WP Rest API V1 in this example but am now transitioning to V2. In your example above are the items in the $movies_custom_fields array custom fields or custom post types? The root of my issue is that I need to be able to include a mixture of multiple post types in a single feed.
They are custom fields. When you get_post_meta, you would have to reference the $object[ 'id' ] of the different post types as well. In this example, I am only referencing the movies post type: register_rest_field('movies',
I have both of my custom post types setup properly and can query them both without issue in separate queries. What I really need is a single query to call a list of a mixture of book and movie post types. This works in V1 but I am unable to get this to work in V2.
2

We had a similar requirement, where we only had a slug and need to query / find the post/page/article data.

We have build a wordpress api v2 plugin to query multiple post types.

Here is the plugin code https://github.com/elevati/wp-api-multiple-posttype

3 Comments

Thanks for posting this! I will have to test it out.
Do you know of any way to do the same thing with tags? E.G. example.com/wp-json/wp/v2/posts?tags[]=1&tags[]=2
Works with WordPress 5.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.