Making a custom column in WordPress admin sort by reverse order

 

It’s known and well-published that you can add custom columns to your WordPress  admin post overview screen. For instance see this article on the codex.Then you can make this custom column(s) sortable,  as described here.

For a recent project I had to make something like that, but including a checkbox with it. By default WordPress will start to sort your columns ascending, from low to high. This checkbox basically has two values, 0 or 1 ( boolean-like ) so starting ascending the posts who were not marked came first, so you had to click twice if you wanted to see tagged posts ( and why sort on a column like that otherwise ).

The solution is a small snippet that’s not really documented:

function checkbox_post_sortable($columns)
{

// second param is sort desc by default
$columns[“checkbox_post”] = array(‘featured’,1);

return $columns;
}

Usually in tutorials the column is defined by $columns[name] => title. By simple making it an array with the second position 1 WordPress will not sort your column descending when you click it first.

 

Tags: , , ,


Bas Schuiling

Bas Schuiling

Expert on everything web development and WordPress. Is pressing his words when version 1 came out and has been liking it ever since. More than 10 years of work experience in web development and writer of this blog.