Search code examples
phpjsonwordpressrestwordpress-rest-api

Can I use the REST API for a site hosted on WordPress.com


I have a blog hosted on WordPress.com. When I try to make a GET request to retrieve the posts I get a 'Not Found' html page.

https://alexkojin.wordpress.com/wp-json/wp/v2/posts

I know about the legacy public API but it doesn't allow me to retrieve private posts.

Can I use the REST API for site hosted on WordPress.com?


Solution

  • On the WordPress.com platform the REST API is actually hosted and structured at https://public-api.wordpress.com/wp/v2 for all sites. In addition, there's a /sites/ endpoint that will namespace your specific site.

    For example, the root endpoint for your site would be https://public-api.wordpress.com/wp/v2/sites/alexkojin.wordpress.com/. The other endpoints behave the same way as they would on a self hosted platform.

    For example, your pages would be found under https://public-api.wordpress.com/wp/v2/sites/alexkojin.wordpress.com/pages

    You can find more information at https://developer.wordpress.com/2016/11/11/wordpress-rest-api-on-wordpress-com/ including how to setup OAuth2 (which will be required to access private posts).