Change Order of Admin Posts Depending on Meta

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.

1 Answer
1

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');
  }
}

Leave a Comment