admin管理员组

文章数量:1125740

It seems that if you use _fields in an API request also containing _embed, the _fields filter will filter out all embeds.

The following request:

domain/wp-json/wp/v2/posts?_fields=link,title&_embed=wp:featuredmedia

has no _embedded field, but it comes back if I remove the _fields filter. I also tried passing _fields=link,title,_embedded, but it doesn't work either.

It seems that if you use _fields in an API request also containing _embed, the _fields filter will filter out all embeds.

The following request:

domain.com/wp-json/wp/v2/posts?_fields=link,title&_embed=wp:featuredmedia

has no _embedded field, but it comes back if I remove the _fields filter. I also tried passing _fields=link,title,_embedded, but it doesn't work either.

Share Improve this question asked Apr 12, 2020 at 15:40 djjeckdjjeck 1931 silver badge6 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

It is not clear in the documentation, but you need to include the "_links" and "_embedded" as fields to be returned. In addition, I include the _embed parameter, as it does not require a value. As of WordPress 5.4, the resources to embed can be limited by passing a list of link relation names to the _embed parameter, though I have not had success with that when using _fields

Example:

domain.com/wp-json/wp/v2/posts?_fields=link,title,featured_media,_links,_embedded&_embed

If you use somebody's REST API and you don't have access to WP website that provides this REST API you should use dfmjr's solution. But this solution has 2 disadvantages:

  1. You have to add additional parameters into every single request
  2. You have to mention all the parameters that you need to be returned, otherwise they won't be returned.

But what if you have your own REST API and you want to return embedded media in your REST API responses every time and don't want to type all the fields you need in your request? Usually I have have such issue when I add a Custom Post Type in functions.php of my theme using register_post_type() function. In this case you should pass the '_links' value of the 'supports' array to your register_post_type(). For example:

<?php
add_action( 'init', 'create_post_types' );

function create_post_types() {
  register_post_type( 'book',
  array(
    'labels' => array(
      'name' => __( 'Books' ),
      'singular_name' => __( 'Book' )
    ),
    'public' => true,
    'has_archive' => true,
    'show_in_rest' => true,
    'rest_base' => 'books',
    'rewrite' => array('slug' => 'book'),
    'supports' => array('title', 'editor', 'thumbnail', '_links')   // <--- INSERT '_links' HERE
  )
);
?>

And you won't need to add additional parameters into your REST API requests anymore:

domain.com/wp-json/wp/v2/posts?_embed

It seems _link is necessary, this is what you can do.

/wp/v2/pages?_embed&_fields=_links.wp:featuredmedia,_embedded.wp:featuredmedia

Or

/wp/v2/pages?_embed=wp:featuredmedia&_fields=_links.wp:featuredmedia,_embedded

Unfortunately I can't manage to make another step of filter for wp:featuredmedia so that the response will only return for example its id or source_url to help minimize the size and make it faster.

本文标签: rest apiHow to use embed when using fields