Changing sort order on list (initial) view not working until second click
-
I set up a simple custom post type, “hiddenemail”, which has two columns displayed in the administrative list view (the default view when selecting the custom post type from the WordPress administrative menu). I managed to be able to get the records to show up in the proper order by default using the following code:
function hiddenemail_default_order( $query ){ if( $query->get('post_type')=='hiddenemail' ){ if( $query->get('orderby') == '' ) { $query->set('orderby','title'); } if( $query->get('orderby') == 'title' && $query->get('order') == '' ) { $query->set('order','asc'); } } } add_action('pre_get_posts','hiddenemail_default_order');
That seems to work well, as items are now sorted alphabetically by the title by default. however if I decide to change the sorting order (by clicking on the arrow next to the column heading), the first click still sorts in ascending order. The second click sorts in descending order.
When I look at the URL that the sorting arrow is pointing to, I see the following:
Initial view: edit.php?post_type=hiddenemail&orderby=title&order=asc After first click: edit.php?post_type=hiddenemail&orderby=title&order=desc After second click: edit.php?post_type=hiddenemail&orderby=title&order=asc
What I need is for the initial view URL to have order=desc (so that it actually changes from the default sort order).
- The topic ‘Changing sort order on list (initial) view not working until second click’ is closed to new replies.