admin管理员组文章数量:1130349
We have a couple of post types which don't directly support authors. Instead, we have custom interfaces for choosing the author.
We end up with an author ID, though, so I'd like to inject that author ID into the REST data for that custom post type.
I've tried this:
add_filter( 'rest_post_dispatch', function( $results ) {
$result->add_link( 'author', rest_url( '/wp/v2/users/42' ), array( 'embeddable' => true ) );
return $results;
});
… but that returns the link on the entire response (e.g. a set of 10 custom posts) -- what I want to be able to do is, for each post, add a link with that post's own user ID.
Is this possible? I can't find documentation anywhere -- the best I found was this old page on Linking.
We have a couple of post types which don't directly support authors. Instead, we have custom interfaces for choosing the author.
We end up with an author ID, though, so I'd like to inject that author ID into the REST data for that custom post type.
I've tried this:
add_filter( 'rest_post_dispatch', function( $results ) {
$result->add_link( 'author', rest_url( '/wp/v2/users/42' ), array( 'embeddable' => true ) );
return $results;
});
… but that returns the link on the entire response (e.g. a set of 10 custom posts) -- what I want to be able to do is, for each post, add a link with that post's own user ID.
Is this possible? I can't find documentation anywhere -- the best I found was this old page on Linking.
Share Improve this question asked Nov 7, 2018 at 22:16 Nabha CosleyNabha Cosley 6883 silver badges11 bronze badges1 Answer
Reset to default 1I found out how to grab each result and add a link. For the ananda_video_audio post type:
add_filter( 'rest_prepare_ananda_video_audio', function( $results ) {
// These two lines are unique to our code of course
$video = new Ananda_Video_Audio( $results->data['id'] );
$authors = $video->get_author_IDs();
foreach( $authors as $author_id ) {
$results->add_link( 'author', rest_url( '/wp/v2/users/' . $author_id ), array( 'embeddable' => true ) );
}
return $results;
});
Because embeddable is set to true, the author data all gets added to _embedded as well, in the response. Works great.
Links I found helpful, in case you're doing something similar:
- Adding links
- List of REST filters in WordPress
We have a couple of post types which don't directly support authors. Instead, we have custom interfaces for choosing the author.
We end up with an author ID, though, so I'd like to inject that author ID into the REST data for that custom post type.
I've tried this:
add_filter( 'rest_post_dispatch', function( $results ) {
$result->add_link( 'author', rest_url( '/wp/v2/users/42' ), array( 'embeddable' => true ) );
return $results;
});
… but that returns the link on the entire response (e.g. a set of 10 custom posts) -- what I want to be able to do is, for each post, add a link with that post's own user ID.
Is this possible? I can't find documentation anywhere -- the best I found was this old page on Linking.
We have a couple of post types which don't directly support authors. Instead, we have custom interfaces for choosing the author.
We end up with an author ID, though, so I'd like to inject that author ID into the REST data for that custom post type.
I've tried this:
add_filter( 'rest_post_dispatch', function( $results ) {
$result->add_link( 'author', rest_url( '/wp/v2/users/42' ), array( 'embeddable' => true ) );
return $results;
});
… but that returns the link on the entire response (e.g. a set of 10 custom posts) -- what I want to be able to do is, for each post, add a link with that post's own user ID.
Is this possible? I can't find documentation anywhere -- the best I found was this old page on Linking.
Share Improve this question asked Nov 7, 2018 at 22:16 Nabha CosleyNabha Cosley 6883 silver badges11 bronze badges1 Answer
Reset to default 1I found out how to grab each result and add a link. For the ananda_video_audio post type:
add_filter( 'rest_prepare_ananda_video_audio', function( $results ) {
// These two lines are unique to our code of course
$video = new Ananda_Video_Audio( $results->data['id'] );
$authors = $video->get_author_IDs();
foreach( $authors as $author_id ) {
$results->add_link( 'author', rest_url( '/wp/v2/users/' . $author_id ), array( 'embeddable' => true ) );
}
return $results;
});
Because embeddable is set to true, the author data all gets added to _embedded as well, in the response. Works great.
Links I found helpful, in case you're doing something similar:
- Adding links
- List of REST filters in WordPress
本文标签: How to addembed an author into REST data from a custom post type
版权声明:本文标题:How to addembed an author into REST data from a custom post type? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749198834a2331451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论