Search code examples
phpwordpresswp-api

WP REST API - Get category name instead of id on post request


is there any solution to get the category name instead of id in the response on following endpoint? /wp-json/wp/v2/posts/:post_id

Thanks Guys


Solution

  • By adding the _embed query parameter to the endpoint, you can also retrieve categories.
    It will be populated under _embeded object and then you can access it under wp:term in the JSON response.
    Here's an example of how to do it for posts:

    {{site}}/wp-json/wp/v2/posts?_embed=
    

    Notice that _embed can get different values. For example, if you only need to embed wp:term (includes categories and tags) in the final result you can do this:

    {{site}}/wp-json/wp/v2/posts?_embed=wp:term