1

I am busy trying to understand how WP REST API works in combination with React. When I check the documentation of WP REST API, it confuses me a bit.

https://developer.wordpress.org/rest-api/reference/comments/#retrieve-a-comment

For example here, it seems like you have to specifically give the ID of a post in the query to fetch the post. Can we not use a variable for this? If so, how could this be done? And how could we make sure that per post, automatically we also fetch its comments beneath it?

I mean, this gets all of the comments:

https://example.com/wp-json/wp/v2/comments

But it does not let me combine this with the posts. How do we relate them with each other?

Another example:

https://example.com/wp-json/wp/v2/posts?categories=33

Would mean that we you fetch the categories of post with ID = 33, but how can we make this generic? Like https://example.com/wp-json/wp/v2/posts?categories=$GET_POST_ID or so?

3
  • That logic belongs in our application. Your app should know which post is displaying and you can use that to request comments for that post by specifying the ID. The rest API is stateless, you need to tell it what you want. It can’t figure that out for you. Commented Apr 5, 2020 at 9:38
  • Yes, but how on earth? I mean, the documentation is incredibly poor (or I just don't get it). Can you help a bit more?
    – Siyah
    Commented Apr 5, 2020 at 10:30
  • It's documented here: developer.wordpress.org/rest-api/reference/comments/… You just need to specify the post argument to limit comments to the specified post. Commented Apr 5, 2020 at 10:36

1 Answer 1

2
https://example.com/wp-json/wp/v2/comments?post=<POST_ID>

will return comments for a specific post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.