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.
3 Answers
Reset to default 17It 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:
- You have to add additional parameters into every single request
- 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
版权声明:本文标题:rest api - How to use _embed when using _fields? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736674003a1947078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论