• 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).

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try that

    
    function wp_change_admin_order_hiddenemail( $wp_query ) {
      if (is_admin()) {
    
        // Get the post type from the query
        $post_type = $wp_query->query['post_type'];
    
        if ( $post_type == 'hiddenemail') {
    
          $wp_query->set('orderby', 'title');
    
          $wp_query->set('order', 'desc');
        }
      }
    }
    add_filter('pre_get_posts', 'wp_change_admin_order_hiddenemail');
    
    Thread Starter midimatt

    (@midimatt)

    Nope… That just sorts them in the opposite direction I want them to be sorted by default. It also prevents any non-default sorting from ever occurring on the list.

    I had them sorted the way I wanted by default – The issue is that the arrow in the column heading (used for sorting) first wants to sort in ascending order, which by default I already had them in… So it takes two clicks (instead of one) to sort in descending order.

    * I did keep the “if(is_admin())” portion of your code, though… I didn’t think to do that before.

    Moderator bcworkz

    (@bcworkz)

    The issue is you’ve changed the query to get what you want, but it is not coordinated with the list table header classes that decide which way the sort arrows initially point.

    Before addressing that, why wouldn’t you simply alter the admin menu URL to include the query vars you want as default? In fact, the URL edit.php?post_type=hiddenemail should result in posts sorted by title in ascending order by default. If it is not, your theme or a plugin is altering the default primary column. It makes more sense to resolve the source of unwanted behavior than to attempt to force what you want when you shouldn’t need to do anything.

    Even if the proper default is not what you want, you could get what you want by altering the admin menu URL that leads to the post list table. Let’s say you wanted posts sorted by ascending date as the initial view. Alter the menu item URL to be edit.php?post_type=hiddenemail&orderby=date&order=asc. No need to fuss with queries or which way the header arrow points.

    OK, I’ve said my two cents worth, now to answer your question even though I don’t think you should have to go to such lengths. Sortable columns have a flag that is set when the initial direction should be descending instead of the default ascending. We typically see this with the date column, but any sortable column can be configured this way. I’m confused about the initial direction, as you are setting the query for ascending but you say you want the arrow to be descending. Well, there’s only two possibilities. If one doesn’t work, do the other ??

    To affect the initial header arrow direction, hook the “manage_edit-hiddenemail_sortable_columns” filter. Mind the hyphens and underscores, there are both, don’t mix them up. Hook with a large priority number to ensure your setting is the final state after other plugins may have made changes that do not match your needs. Your callback is passed an array of sortable column data. I’ll assume you collect the data as $columns. Either do one or the other of the following before returning the entire array:

    // for ascending
    $columns['title'] = 'title';

    OR

    // for descending
    $columns['title'] = array('title', true,);
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Changing sort order on list (initial) view not working until second click’ is closed to new replies.