Filtering

Enabling Filters for Meta Fields in the WordPress Rest API.

WordPress has been an excellent platform and as of now it is gaining popularity as a headless CMS. And I too have chosen to keep WordPress as a headless CMS. And why not WordPress has lots of great features and a very active development community.

I mean no software is perfect and the best next thing to a software is to have something which have lots of active development going on. This way one has lots of support for the software. In in todays world software versions are released more frequently than even and the eco system of development relies on a lot of 3rd party tools and services hence we have to keep the things upto-to-date with these 3rd party tools so in my opinion WordPress is a very good option to chose from.

Secondly the WordPress is a world class CMS. No wonder why WordPress contributes to 36% of the total market share for the websites in the world.

And I will contribute to its Architecture which allows it to be extended without touching the Core System. And this is very much required in todays world because there so many developers and each developer has a different level of experience and through process. So if they change/tamper the original code it would be very difficult to track the changes and also revert them.

Even if you would be able to revert the code still their are chances that it could be improved there. But in case of WordPress they can make small plugins and install them. If something goes wrong you just can turn off the plugin (or deactivate it ) and puff all the problems are gone.

Not only that, this way they developers can independently develop plugins on a separate version of WordPress, test it and then put it on the Original Website. Also it is easy to upload the plugins through the in built system.

All these are the primary reason which has encouraged me to take WordPress as the platform of choice for the current project I am working on.

REST API

Rest API is which makes WordPress Quite a good candidate to work as a Headless CMS, will in my current project not only I am using it as a Headless CMS but more over I am building a software over it which not only Reads the data from the headless CMS but it also writs beck data to it.

But my experience is not straight forward there are some things which do not work out of the box. Like I have been using the WordPress by using Custom Post type and I have extended it with my own field with meta fields.

Now here while I am Reading, Writing data every things works fine out of the Box, but when I am reading data I have some requirements such as ordering the data with the meta fields and also filtering the data.

To achieve ordering of the data based on the meta field I had to use Filters and it was achieved. If you are looking forward to do so the article I wrote about it the following is the link.

Now the Second problem that I am facing is how to filter the records based on meta fields. Now I started by research on the internet and I found on the documentation of the WordPress that the filtering option existed in the REST api but now it has been removed from it in the latest versions. The also suggested that there are plugins available which can enable the same functionality again. Now this was disappointing because I love when the WordPress by default provides the functionality because that way I am sure they are going to keep the functionality maintained in the future.

But I had no other option available than to write the functionality yourself.

I found out that the WordPress documentation requests you to write a custom endpoint yourself and then you can get you customized results. Now this is an option but it would been I would have to write all the endpoints myself and this would mean a lot of work.

I needed something which is more like a generic solution enabling the filtering by default.
Here is when I found this plugin.


https://github.com/WP-API/rest-filter

Now it was a great help, the plugin works as intended but writing queries it is difficult because you can write nested queries. So it is very powerful and it works but the problem is that its difficult to form queries.

So for my own purpose I added a little change, instead of sending queries directly as POST variables, I made changes so that I can send them through a JSON Variable.

If anyone needs to do so they can use the following code.