For the background to my question: On my page listing, I've got a set of pages that are countries and the child pages of those are categorised into 'Types'. So I've got a page structure like this:
Country 1
-- Resource
-- Resource
-- Organisation
-- Overview
Country 2
-- Overview
-- Overview
-- Overview
-- Organisation
Country 3
.
.
.
I've added in some additional filters to display only child pages of a selected country and when this filter is in action, an additional column is shown to show what 'Type' the page is. I've also added the necessary code to make this column sortable. So my code is as follows:
// Add Type column
add_filter('manage_pages_columns', 'my_columns');
function my_columns($columns) {
if(!empty($_GET['country']) && empty($_GET['type'])) {
$column_type = array('type' => 'Type');
$columns = array_slice($columns, 0, 2, true) + $column_type + array_slice($columns, 3, NULL, true);
}
return $columns;
}
// Populate Type column
add_action('manage_pages_custom_column', 'my_show_columns');
function my_show_columns($name) {
if(!empty($_GET['country']) && empty($_GET['type'])) {
global $post;
switch ($name) {
case 'type':
$type = get_field('type', $post->ID);
echo $type;
}
}
}
// Make Type column sortable
add_filter('manage_edit-page_sortable_columns', 'my_website_manage_sortable_columns');
function my_website_manage_sortable_columns($sortable_columns) {
if(!empty($_GET['country']) && empty($_GET['type'])) {
$sortable_columns['type'] = 'type';
}
return $sortable_columns;
}
// Add sort logic
add_filter('request', 'type_column_orderby');
function type_column_orderby($vars) {
if (isset($vars['orderby']) && 'type' == $vars['orderby']) {
$vars = array_merge( $vars, array(
'meta_key' => 'type',
'orderby' => 'meta_value'
));
}
return $vars;
}
So I can sort the Type ASC or DESC fine, but how would I go about reverting to the default sorting of the page listing where it shows the indented hierarchy of the pages?
!empty($_GET['country']
So when it is not set then it is good to list the pages in hierarchy! isn't it ?