How to fetch posts from my WordPress blog without accessing the database

If you view the websites being developed nowadays you will find an option for a blog or blog posts to be shown on the web page. Blog posts keep your website content up to date and search engines like websites which
regularly post content on their website.

If your blog is on the same server and running in a sub-folder like www.example.com/blog then you don’t need to do a lot however if your blog is hosted on another URL like blog.example.com or if you don’t have access to the database from where you can fetch the posts and display the latest posts then you need a way to do so.

Luckily WordPress provides a solution for these type of instances. WordPress provides the REST API through which you can request the posts you want to display on your website. The REST API provides a number of end points. As we need to display the posts we will use the posts end point.

To access all the posts you can enter the following URL in your browser

https://www.example.com/wp-json/wp/v2/posts

The above URL will display all the posts in JSON format. You can write a script which can read the JSON, parse it and then display on your web page.

If you would like to search for a particular post you can change the above URL to

https://www.example.com/wp-json/wp/v2/posts?search=abc

The above URL will only display those posts which have the term “abc” in it.

In addition to the above URL WordPress also provides a number of end points to fetch tags, categories etc. Some of those URLs are given below

https://www.example.com/wp-json/wp/v2/categories
https://www.example.com/wp-json/wp/v2/tags
https://www.example.com/wp-json/wp/v2/users

Hope the above helped. If you have any questions/comments then please let me know.