0

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?

2
  • What order you want by default? Because your custom column is depended on condition !empty($_GET['country'] So when it is not set then it is good to list the pages in hierarchy! isn't it ?
    – Sumit
    Commented Mar 15, 2016 at 17:23
  • When I filter by country then sort by type, I want to be able to go back to being listed in the hierarchy (as it is by default) if i remove the filter. That's what I'm unsure on how to achieve. Hope that makes sense!
    – Adam Lobo
    Commented Mar 15, 2016 at 17:38

1 Answer 1

1

By default pages are order by menu_order then title. When you are adding filter country you are ordering them by meta_value

There are two options

  1. Remove everything that you added country and orderby pages will be back to default order.
  2. If you can not remove orderby but can remove country then adjust your code in this way and order pages again by menu_order title only when country is not set and orderby is type.

Updated code:-

function type_column_orderby($vars) {
    // You can also wrap this code in one more condition using $vars to only alter page list!
    if (!empty($_GET['country']) && isset($vars['orderby']) && 'type' == $vars['orderby']) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'type',
            'orderby' => 'meta_value'
        ));
    } else if (empty($_GET['country']) && isset($vars['orderby']) && 'type' == $vars['orderby']) {
        $vars = array_merge( $vars, array(
            'orderby' => 'menu_order title',
            'posts_per_page' => -1
        ));
    }
    return $vars;
}

NOTE: request filter effect every query so wrap your filter in load-edit.php action to only alter the post type listing query.

add_action( 'load-edit.php', 'custom_page_order' );
function custom_page_order() {
    add_filter('request', 'type_column_orderby');
}
4
  • Getting some weird behavior now - When I filter by country, sort by type and then remove the county filter (which is the point I want it to go back to the default listing), it's only bringing back exactly 100 rows and no pagination..
    – Adam Lobo
    Commented Mar 16, 2016 at 9:45
  • 1
    @LoboDemon Not sure why but you also need to set the 'posts_per_page' => -1 because default query has it. Updated my answer.
    – Sumit
    Commented Mar 16, 2016 at 12:39
  • That's sorted it! Thanks so much. What was also weird, was that it wasn't even the first 100 rows that were being returned..
    – Adam Lobo
    Commented Mar 16, 2016 at 13:16
  • Glad it works! Not sure why it behaves like that I will debug this and comment here! Good Luck :)
    – Sumit
    Commented Mar 16, 2016 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.