Please wait...

Sorting tables in WordPress

Video versionA video overview of sorting tables in wpDataTables

wpDataTable sortable tablesHow does sorting in tables work

wpDataTables sorting

Tables rendered by the wpDataTables plugin are sortable by default. It means that the content of the table can be ordered by the values of one of the columns. The ordering rules depend on the column type: e.g. string columns will be ordered by alphabetical rules, integer or float by arithmetic rules, and so on. See “Column Types and Features” chapter of this documentation for detailed info on different column types, and sorting rules. To enable sorting (or to disable, since it is enabled for all columns by default) you need to use the “Allow sorting for the table” switch in the “Sorting and filtering” tab in the table configuration page.

Sorting one columnHow to sort by a single column

When the sorting is enabled, each column header will get “interactive”: it will work as a button, and when you click it, you will sort the table by this column in ascending (increasing order), which will be indicated by the highlighting of the column (means that it is used for sorting), and by the triangle showing to the right of the header turning black. When triangle is showing “up” it means that the table is sorted in ascending order – from “smaller” to “bigger” value:

wpDataTables sorting ascending

When you click the same header again, the triangle will turn “down”, which means that the sorting is done in descending order from “bigger” to “smaller” value:

Sorting multiple columnsHow to sort by multiple columns

If you sort by one of the columns, then press “Shift” button on the keyboard, and, while holding it, click on another header, it will be used as a second sorting column (first the ordering will be done based on the first column, and then based on the second column). The secondary sorting means that when the values of the first columns are the same for a number of rows, you can also use some other column values to get the order that you want.

In this example we used “Exam name” as first sorting column and “Student name” as second sorting column.

wpDataTables sorting multiple columns

Individual column sortingConfiguring default column sorting by a given direction

wpDataTables sorting individual columns

If the sorting is enabled, one of the columns will be used for ordering when you open the page (“default sorting column”). If you don’t provide it manually, the first column will be used for ordering. If you want to use a different one, you can define it in the column settings. Each column has a “Default sorting column” switch. If you enable this switch for one of the columns, a selectbox will appear with  options – “Ascending” and “Descending” where you can choose sorting direction. Column, with “Default sorting column” option enabled, will be used for ordering when front-end users first open the page.

From version 2.0 it is possible to toggle sorting per individual column. If you want to disable sorting per column you should disable “Allow sorting for this column” option on the column settings panel.

If you want to disable sorting for all columns but to have “Default sorting column” feature, then “Allow sorting for the table” option have to be enabled on the table settings and you have to disable “Allow sorting for this column” for every column separately.

Never miss new features!

Join 2000+ newsletter subscribers

Never miss notifications about new cool features, promotions, giveaways or freebies - subscribe to our newsletter! We send about one mail per month, and do our best to keep our announcements interesting.

We never spam or disclose your address to anyone.