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: , , ,


'float:left')); ?>