admin管理员组文章数量:1208155
When you request posts using WP API's search mechanism, it returns tags as one of the post's properties, but it is an array of tag IDs, not tag names. Is there a way to make the API include the name of the tags without making subsequent requests to the API by tag ID for each one?
tags: [
188,
30,
151,
189
]
I couldn't find any parameter in the API docs that does this, so I was thinking of creating a custom plugin to maybe filter and substitute the tag ID's for names before the response is sent back to the API caller. In that case, what action should I listen for?
When you request posts using WP API's search mechanism, it returns tags as one of the post's properties, but it is an array of tag IDs, not tag names. Is there a way to make the API include the name of the tags without making subsequent requests to the API by tag ID for each one?
tags: [
188,
30,
151,
189
]
I couldn't find any parameter in the API docs that does this, so I was thinking of creating a custom plugin to maybe filter and substitute the tag ID's for names before the response is sent back to the API caller. In that case, what action should I listen for?
Share Improve this question edited Oct 6, 2016 at 20:58 The Unknown Dev asked Oct 6, 2016 at 20:26 The Unknown DevThe Unknown Dev 3515 silver badges15 bronze badges 5- Please, make more clear from start that you need a solution to implement on the server side. – ClemC Commented Oct 6, 2016 at 22:05
- @ClemC Will do next time. But writing plugins is almost always a server side solution, no? – The Unknown Dev Commented Oct 7, 2016 at 1:54
- I mean in the context of REST API requests, you can have a WP server and a WP client.... – ClemC Commented Oct 7, 2016 at 10:00
- I ask you this because your good question will be read by futur readers having potentially the same issue as yours and for who you can bring a good solution. So make clear from start (ex: in your title) to help them understand faster your issue. Just a little optimization. :) – ClemC Commented Oct 7, 2016 at 10:11
- Okay, I got it thanks! Please feel free to edit my title in a way that makes it easier for future developers. Thanks for the feedback. – The Unknown Dev Commented Oct 8, 2016 at 0:01
2 Answers
Reset to default 7I figured something out based on what I found at this post.
Basically I need a plugin that listens for when the REST response is about to go out. The plugin code would be similar to the following:
function ag_filter_post_json($response, $post, $context) {
$tags = wp_get_post_tags($post->ID);
$response->data['tag_names'] = [];
foreach ($tags as $tag) {
$response->data['tag_names'][] = $tag->name;
}
return $response;
}
add_filter( 'rest_prepare_post', 'ag_filter_post_json', 10, 3 );
It adds the tag names as a new property called tag_names
. The rest of the heavy lifting is done by the wp_get_post_tags
function.
The approved answer wasn't working for me in Wordpress 5.9. Changing $tags = wp_get_post_tags($post->ID);
to $tags = get_the_tags($post->ID);
did the trick.
Also, if you need the slug rather than the name, just update $response->data['tag_names'][] = $tag->name;
to $response->data['tag_names'][] = $tag->slug;
本文标签: WP API Get post with tag names instead of tag ID39s
版权声明:本文标题:WP API Get post with tag names instead of tag ID's 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738715505a2108451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论