7

I am trying to change the default sorting order of the pages in my WordPress backend. I know this can easily be done by clicking on the tab "Title", "Date" or "ID" but those are merely one-time settings and I need a global = default solution.

I went ahead and tried using this function which to me makes perfect sense but it just doesn't work with WordPress 4.2.3 :-(

function set_post_order_in_admin( $wp_query ) {

global $pagenow;

if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {

    $wp_query->set( 'orderby', 'title' );
    $wp_query->set( 'order', 'asc' );       
}
}

add_filter('pre_get_posts', 'set_post_order_in_admin', 5 );

Any idea why this is not working any more? How can I achieve that?

Thanks + regards, Henning

2 Answers 2

10

Just change order "ASC" to "DESC" in your own code, it will work perfectly. Or copy and paste below mentioned code into your functions.php :

function set_post_order_in_admin( $wp_query ) {

global $pagenow;

if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {

    $wp_query->set( 'orderby', 'title' );
    $wp_query->set( 'order', 'DESC' );       
}
}

add_filter('pre_get_posts', 'set_post_order_in_admin', 5 );
4
  • Hey Sunil, thanks for your suggestion! I tried your code but no changes... Have you tried / verified it in your environment? First of all it seems we'd need to use DESC instead of DSC, see WordPress Codex and secondly I don't care so much whether it is ascending or descending but the sorting just doesn't function at all... Any further ideas?
    – Leander
    Aug 1, 2015 at 11:40
  • @Leander thanks for updating me with wordpress codex and you are right. But I have already tested the code with one of my updated wordpress 4.2.3 before answering to you, infact I have created an article as well, you can see here : wptricks24.com/…. May be there is any issue with your current theme, just switch your theme to default theme and then try.
    – WpTricks24
    Aug 1, 2015 at 13:21
  • I just tested it with another website / theme and you are right! No problem at all there, works fine. Will investigate further why this is in conflict with my current theme and get back to you / update this post. Thanks!
    – Leander
    Aug 4, 2015 at 12:47
  • My bad... This code works fine, I just had "Post Types Order" installed and one setting in the options panel overrides the admin sort-ability... Thanks for your help!
    – Leander
    Aug 4, 2015 at 18:39
0

Use this snippet of code :

  function set_post_order_in_admin( $wp_query ) {
    global $pagenow;
      if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
        $wp_query->set( 'orderby', 'title' );
        $wp_query->set( 'order', 'DSC' );
      }
    }
    add_filter('pre_get_posts', 'set_post_order_in_admin' );
1
  • Thanks – see my comment to Sunil's post above, same here.
    – Leander
    Aug 1, 2015 at 11:42

Your Answer

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

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