3

I have created a number of 'views' column in the admin area of my posts. There are several tutorials for this and I did in fact use a number of these to arrive at my end result.

It works as expected but I would like the initial sort order to be numeric descending, but unfortunately when you first select it, it is always ascending. Clicking it again gives you a descending list. I would like for it to be initially descending when first selected, and then if you want ascending (which is unlikely) I will click it again.

I have tried a number of solutions (all of my own device) but none that work 100% as required.

Here is my initial code which sorts ascending then descending:

add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    if( 'view' == $orderby ) {
        $query->set('meta_key','views');
        $query->set('orderby','meta_value_num');
    }
}

My initial solution was to add a sort order:

add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    if( 'view' == $orderby ) {
        $query->set('meta_key','views');
        $query->set('orderby','meta_value_num');
        $query->set('order','desc');
    }
}

This kinda works, but it is ALWAYS descending, since I am setting it that way.

So then I made it conditional:

add_action( 'pre_get_posts', 'my_views_orderby' );
function my_views_orderby( $query ) {
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    if( 'view' == $orderby ) {
        $query->set('meta_key','views');
        $query->set('orderby','meta_value_num');
        $orderdir = $query->get( 'order');
        if('desc' == $orderdir) {
            $query->set('order','asc');
        } else {    
            $query->set('order','desc');
        }
    }
}

Now this works, but the sort indicator is the wrong way around. ie when it is sorted ascending, the icon is indicating descending and vice versa. When I hover over the sort column, the icon indicates the PRESENT state and not the state it will become. Also, the link url (which appears at the bottom of chrome) is telling me it is going to sort by the incorrect direction. What this means, if I am already sorted by views descending, the link indicates it will sort by descending, and v.v.

In the off chance it's relevant, this is how I have declared the column as sortable:

add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_view_column' );
function my_sortable_view_column( $columns ) {
    $columns['post_views'] = 'view';
    return $columns;
}
0

Instead of adding your column label "view" as a string, pass it in as an array with 1 as the second value. Like this: array('view',1)

Full Code:

add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_view_column' );
function my_sortable_view_column( $columns ) {
    $columns['post_views'] = array('view',1);
    return $columns;
}

I don't have a WP Codex for this, but I found someone else posted a solution here: http://www.weblogmechanic.com/2014/02/13/making-custom-column-wordpress-admin-sort-reverse-order/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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