1

I am fetching data from my WordPress site's API, and I need to embed the data from the 'author' field in order to retrieve author name, as by default only the author ID is returned.

However, when I use the _embed global parameter to embed the data for the author, I get a bunch of additional data I do not need. I only want the author name.

My URL is: my_site_for_example_purposes/wp-json/wp/v2/posts?_fields=id,title.rendered,link,date,author,excerpt.rendered,tags,categories,yoast_head_json.og_image,_links.author,_links.wp:term,_embedded&_embed=author

I have tried limiting the fields using author.name, _embedded.author.name, _embedded.author[0].name, etc. but I still receive the JSON response containing all the unneeded information (id, avatar_urls, slug, etc.)

I believe it is because the embed looks like this and I cannot index the lists in my URL parameters (i.e. _embbeded.author[0].name):

'_embedded': {'author': [{'_links': {'collection': [{'href': 'https://my_site/wp-json/wp/v2/users'}],
                                      'self': [{'href': 'https://my_site/wp-json/wp/v2/users/1647'}]},
                           'amp_dev_tools_enabled': False,
                           'amp_review_panel_dismissed_for_template_mode': '',
                           'avatar_urls': {'24': 'https://secure.gravatar.com/avatar/url',
                                           '48': 'https://secure.gravatar.com/avatar/url',
                                           '96': 'https://secure.gravatar.com/avatar/url'},
                           'description': '',
                           'id': 1647,
                           'link': 'https://my_site/author/author-name/',
                           'name': 'Author Name',
                           'slug': 'author-name',
                           'url': '',
...

I would really like to avoid fetching all this unnecessary data when all I want is the name attribute. Is it possible to do this in one request, or do I have to make a separate request with the authors endpoint?

linguini
  • 44
  • 4

0 Answers0