Fast understanding of powerful Rest API in WordPress in just 15 minutes

In many conditions during our website developemnt process we need to do something dynamic with our content this is where Rest API in WordPress comes to help. The usage of REST API starts form advanced search up to the posts/products live filtering and in other sphears.

Let’s imagine you need to make custom product filtering on your WooCommerce shop page(remmeber that products of woo commerce are teh same post types as ), if every time when filter is being applied the page will be refrshed it will make a lot of technical difficulties, this is where we start using Rest API.

What is API?

Let’s refresh our memory and remember what is API. API helps us to connect two applications. For example showing posts on single.php, which is powered bt WordPress API, or showing information kept in shortcodes, which is powered by Shortcode API.

Rest or Restful API helps us to unleash full power of CRUD(create, reaf, update, delete) in our WordPress pages, which basically means that we can do everything we want with our content.

rest api in wordpress
Row format Rest API in wordpress

How to enable rest api in wordpress?

Rest API in WordPress is not blocked, so there is no need to enable rest api in wordpress, but is you are having problems with acessing Rest API in the ways described below, it means that a plugin(in most cases you might have turned on blocking feature in security plugins) is blocking Rest API, make sure to turn off the block form your WordPress admin dashboard.

Additionally it might be problems with your server, so if the case explained above does not solve problems, it means you shall check it from your server providers. Mostly AWS hostings tend to cause this sirt of probems, that is why I usually don’t recomend using AWS hosting s for WordPress. It is much mre expensive like usual hostings like Hostinger or Bluehost or others in the niche.

How to access WordPress Rest API?

What you see above is the row format of rest api information which you can always find out using following link yourlink.com/wp-json/wp/v2/posts(if you want to access posts) or yourlink.com/wp-json/wp/v2/pages(you shall type in the end the post type name you would like to use, that can be also custom post types as well).

All the contnet in in JSON format(Java Script Object Notation), which is a popular format for structuring the data. But from the first sight is seems a bit messy and all the content is smushed together. For solving this problem we can use a ncie apcation called Postman.

As you can see in images above the data is well structures. Just make sure to put the correct URL, with the structure manetioned above yourwebsite.com/wp-json/wp/v2/posts(or the name of your post types), you can also have other URL endings of course if you have created one for your custom purposes. Here you can read when and how to create custom Rest API endpoints in WordPress.

As you can see in the second image is makes using Rest API in WordPress a lot easier, and we can see that we have clear distinction between all associative arrays that we have from JSON get request.

Hereby I want to mention that you don’t actually need to use Postman, it just makes easier to read the array of objects which makes your life a bit easier.

Getting response from Rest API in WordPress according to our requirements

What we have seem above is just a usual response form WordPress, which shows us all the inforamtion about the latest 10 posts, but the truth in the most cases this is not what you need, so we need to find a way which will help us to get the contnt we exactly need.

WordPress Rest APi examples

Here are a few tips how to use Rest API in the way we want, not in the way WordPress returns the content to us.

1. By ID of the post

Let’s say you need to return the content of the ID of the post the user has chosen you simply need to use the following structure: yourwebsite.com/wp-json/wp/v2/posts/348(the last number is the number of post you have chosen).

how to access wordpress rest api
How to access WordPress rest API – finding the post by ID

Note by the author: This example won’t make sense to you until you know how to ue. THe example of whoch can be for example a dropdown of posts where the user chooses thw content of which post to show, where you are retreiving the post content or any other information by the ID of the post, which is the most convinient way of doing it.

2. Searching for something

Another winderful usage of Rest API is showing results of search by the user without reloading the page. To do this you simply need to structure your dat in the follwing way: https://wp-frogs.com/wp-json/wp/v2/posts?search=wordpress(in the end after equality sign comes what the user searches for).

Note by the author: You can follow my tutorial of how to make advanced search using Rest API here. It requires from you strong Java Script knowledge.

3. Using WP_Query arguments

Another nice way of sorting the things in any way you like is using the argument. These are the same we use when making WP_Query arguments. Here is an example: https://wp-frogs.com/wp-json/wp/v2/posts?category_name=website-development.

You can find full list of arguments here.

Conclusion

Here we had covered the basics of Rest API, how it helped you to understand how it works. You can follow to more example here. Or continue to search for other topics in my blog.

Katen Doe

Hovhannes Verdyan

Hello, I’m a developer who loves WordPress and loves helping peopls let me know if you have any question by leaving your question in comments in one of my youtube videos. Click here to go to my channel.