DataTables Server Side Processing in WordPress

DataTables is a very neat and powerful jQuery plugin. Showcasing data in tables with it is easy and the user experience is great. I recently had to use DataTables for a project to showcase a large number of data. For a large number of data, we will need to use the server side processing method for better performance and efficiency. Let us see how to use DataTables server-side processing in WordPress.

Why Not Existing Plugins?

I thought using WP DataTables would be fine. However, it did not work as expected and was very very slow. So, I decided to do it without a plugin. It has its benefits but the overall performance and experience were not satisfactory for a premium plugin.

The Scenario

We are going to build a table using DataTables on a WordPress site which has a lot of movies (over 4800) as the custom post type (I previously used this example on Simple Ajax Filter or Search for WordPress Post). With all that posts, it is not suitable for us to do WP_Query with no limits. That is why we are going to use the server-side processing method to show the data. You can learn more about the server side processing method of DataTables here.

Initial Setup

For the purpose of dataTables search to work properly, we will need to have an extra custom field on each post where the value is equal to the title. We will later use this metadata in our query. Add the following code to your functions.php file to achieve this.

In this code, we are running a function every time a movie post type is saved through transition_post_status action. So, each time the post status changes, for example – draft to published, published to draft etc. The post title will be saved on d_title custom field. This will make the process much simpler later when we will run the query.

Once that is done, I am assuming we have the movies posts created with some custom fields like year, rating, language etc. For simplicity, we are going to show 3 columns on our table. These are Movie Name, Year and Rating. So, let us assume we have these posts ready for showing the data. If you want to know how to set them up, you may check my previous post here.

Adding DataTables Resources

The next thing to do is adding DataTables resources. Add the following code to your active theme’s functions.php file.

Here, we included DataTables Javascript file along with CSS file. For styling, we added Bootstrap’s CSS library and DataTables Bootstrap Javascript file. You may use different libraries for the style. If you already have Bootstrap’s CSS library loaded, you should not need to add the bootstrap.min.css file.

The JS Part

Much similar to our previous tutorial about Ajax filter and search, we are going to add a function and enque script in our shortcode function. Add the following code to your functions.php file

Line 01 & 02: We are adding necessary ajax action to the callback function.

Line 06: We are declaring the header as JSON.

Line 08: We will be getting values from $_GET which will be triggered by the JS code we added earlier.

Line 10: Setting table columns to key or meta.

Line 16 – 22: Our array for the query. We are queriying for movie post types that are published. Important things to notice are ‘posts_per_page’, ‘offset’ & ‘order’. These 3 are needed to set according to the request received from DataTables. You can check the request from the network tab in developer tools (chrome).

$request[‘length’] is giving us how many data the table will show. This is set to ‘posts_per_page’ as it works the same way.$request[‘start’] is giving us from which element to start the query. So, we used ‘offset’.$request[‘order’][0][‘dir’] is giving us the direction for order. ASC or DSC. Here used ‘order’.

Line 24 – 33: Here we are checking if the table was requested to be sorted by the column [0] which is our Movie Title column. Again checking if it is for the column [1] or [2].

$columns[$request[‘order’][0][‘column’]] in short is getting the key or meta (set in line 10) that should be used to sort the query.

Line 37: From this line, we are handling the request for search terms. We are checking if $request[‘search’][‘value’] has any value in it, which indicates if we have a search or not. As we have 3 columns on our table, we will need to search within them. From the search term, our query should look for the match in data and get results. For this, we are going to use ‘meta_query’ and the ‘LIKE’ comparison. Now the function we had set at the very beginning will come in use. As the ‘post_title’ is not a metadata, we can not do ‘meta_query’ with it. So, we are using our ‘d_title’ custom field which has the same value of the title.

Line 59: We are counting the total amount of posts. This is a required data for the table.

Line 61: From here we have the loop that is storing data to $data array.

Line 74 – 81: We add all the required info needed to pass as the result and encode the $json_data array to JSON.

Line 85: For query without any results, we are sending an empty array.

The Result

Let me know in the comments or live chat if you have any questions. If you have any suggestion, feel free to share.

Hi! i am using the Alphabetical Search plugin for DataTables. (https://www.gyrocode.com/articles/jquery-datatables-alphabetical-search/) i implemented your tutorial and it works great (only search does not work but i will figure it out). This alphabetical search does not work with server side. Do you have any tips for implementing an alpha search along with your tutorial?