How to create custom endpoints for custom tables on WordPress

In this post we are going to show you how easy is to create custom endpoints for any custom database table on WordPress through Rest Routes Pro

By Adriano Ferreira

Custom Post Types and the Power of WordPress

 

One of the great things on WordPress is its extensibility, this is a fact. You can extend it through plugins, themes leveraging the power of it. Another thing that you can easily extend on WordPress is the custom post types.

 

Creating custom post types is something that takes WordPress to a whole new level of customization in terms of CMS features. You are not limited to posts and pages.

 

 

The downsides of keeping everything on native WordPress tables

 

So you may think, why do I need custom database tables when I can create infinite custom post types for my site? The answer is pure and simple, performance!

 

If you had the opportunity to work with some large WordPress database, you may have noticed that the tables responsible for storing posts (posts and postmeta) can become monsters in terms of size.

 

It is not rare that when working with those large database tables, you may need to do some SQL queries using LEFT JOIN or RIGHT JOIN and this can be a total disaster.

 

Some things you can do to resolve that problem:

 

  • Partition your database table in some pieces: the problem with this approach is that it becomes a nightmare and sometimes impossible to do SQL JOINs
  • Create some replicas: this helps with the SELECT operations as you can spread the load between the slaves, however, you still have to run UPDATE and DELETE in the master replica
  • Create custom tables for different purposes and that’s what we are going to discuss now

Custom REST endpoints for custom database tables

 

I’m not going to detail the creation of custom tables on WordPress, you can have a look at the official documentation to understand all the details.
 
We are going to focus on the creation of custom endpoints for already created custom tables through Rest Routes Pro.

Defining our custom endpoint for the custom WordPress table

 

First of all, let’s create our new route giving it a namespace and a route name:

Now we have the select the right method for this which is going to be the GET.

Also choose the endpoint purpose which is “Display Custom Table Entries” as you can see below:

Let’s also add some custom parameters that in the future will help us to refine the search results of our custom endpoint:

Now we are going to select a table we’ve previously created, “cars”:

Select now the “Filter Columns” tab and let’s now add some filters accordingly to the image below. This will let us refine the search using all the custom parameters that we’ve previously created, name, year and color:

In the Endpoint Output section, let’s add name, year and color as fields to be exposed by the endpoint:

Save it and it is done! You custom endpoint for the cars custom WordPress table is now available. Let’s access it like shown in the image below:

You should see all the cars stored in the cars table, like the image above.

Now let’s refine our search as the image below:

Nice! The filters worked just fine!

Feel free to go back in the editing page and play with some other options, like Limit & Offset and Ordering.

Conclusion

 

We’ve just created a custom REST endpoint for an arbitrary custom WordPress database table. Now, you are able to expose routes for any kind of table, not only WordPress default tables anymore. I hope you enjoy this feature as much as we did while developing it.

 

 

Does this plugin sounds like a good fit for your site? Click here and start building awesome routes for your site now.

Join our 322,204 newsletter subscribers and get the latest news and articles sent straight to your inbox weekly


Rest Routes 5.5.0

Rest Routes 5.5.0 Insert custom URL parameters values on your Custom SQL Query endpoint type and much more… PRO PRICING By Adriano Ferreira New Custom

Read More »

Rest Routes 5.3.0

Rest Routes 5.3.0 Now you are able to perform authenticated requests through Basic Authentication. PRO PRICING By Adriano Ferreira Basic Authentication is not available!  

Read More »

Rest Routes 5.0

Rest Routes 5.0 Write Custom SQL Queries and make HTTP requests PRO PRICING By Adriano Ferreira We are pleased to announce the 5.0 release of

Read More »