1

In my Admin Panel I have a custom post type of posts - with page attributes to define order. Is there "easy" way of changing how the admin panel pulls posts so I can reorder them?

Page Attributes define order and parents. My Custom Post Type isn't a page but it still supports Page Attributes because I want to order them in a meaningful way.

Currently in Wordpress Admin Panel when using Custom Post Types and Page Attributes, the Admin Panel doesn't order posts in the defined order, it instead orders all posts by publish date (so newest on top). I'm looking to use Page Attributes to order my Custom Post Type posts in the admin panel in the same order defined by Page Attributes.

Even more clarification:

Defined in register_post_type() as supports => array() Found Here

Shows where to find Page Attributes when inside Edit CPT

The expected outcome would be something like this, where the post with page attribute 0 is at the top and the greatest page attribute would be at the bottom:

Shows an example of the expected output with the Order set.

7
  • Can you elaborate on "define order" and "reorder"?
    – brasofilo
    Commented Nov 20, 2013 at 2:38
  • Well right now the posts are showing up top to bottom by latest created. I want to change the order so that they're ordered by the page attribute "Order" I defined. so that 0 will be at the top and 999 will be at the bottom.
    – Howdy_McGee
    Commented Nov 20, 2013 at 14:42
  • What is a "page attribute"? What is your custom post type? You're not giving enough information for somebody to give you a meaningful answer, much less code to do it.
    – Otto
    Commented Dec 5, 2013 at 19:38
  • Page Attributes define order and parents. My Custom Post Type isn't a page but it still supports Page Attributes because I want to order them in a meaningful way.
    – Howdy_McGee
    Commented Dec 5, 2013 at 19:43
  • Making your custom post type hierarchical allows for this sorting natively. You'll have to do a pre_get_posts to change this for your list table. Commented Dec 5, 2013 at 20:00

1 Answer 1

8
+100

Post list in admin (edit.php) use a normal WP_Query, just like frontend can be changed using pre_get_posts.

add_action('pre_get_posts', 'reorder_my_cpt');
    
function reorder_my_cpt( $q ) {
  if ( !is_admin() || !$q->is_main_query() ) {
    return;
  }
  $s = get_current_screen();
  // change 'book' with your real CPT name
  if ( $s->base === 'edit' && $s->post_type === 'book' ) {
    $q->set('orderby', 'menu_order');
    $q->set('order', 'ASC');
  }
}
5
  • This looks promising, im going to run it through a few cases! Thanks!
    – Howdy_McGee
    Commented Dec 5, 2013 at 20:07
  • You're welcome. And glad it works.
    – gmazzap
    Commented Dec 5, 2013 at 20:08
  • Works in 2021! (Surprised this is not the default behaviour?!)
    – 00-BBB
    Commented May 6, 2021 at 10:25
  • 1
    Important note - the code in the answer did break the frontend of the website - 'PHP Fatal error: Uncaught Error: Call to undefined function get_current_screen()'. To fix this I just had to move the 'is_admin()' check outside the 'get_current_screen()' function.
    – 00-BBB
    Commented May 6, 2021 at 10:33
  • 1
    Thanks @00-BBB, I adjusted the code.
    – gmazzap
    Commented May 12, 2021 at 18:18

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.