Skip to content

Instantly share code, notes, and snippets.

@ibrokemywp
Last active August 29, 2015 14:09
Show Gist options
  • Save ibrokemywp/2b1cfd0515076e631eaf to your computer and use it in GitHub Desktop.
Save ibrokemywp/2b1cfd0515076e631eaf to your computer and use it in GitHub Desktop.
Sort your custom post type via pre_get_posts like this
/**
* Check before sorting your custom post type admin lists so you don't
* override other changes.
*/
add_action( 'pre_get_posts', array( $this, 'admin_order_posts' ) );
public function admin_order_posts( $query ) {
if( ( is_admin() && $query->is_admin ) && $query->get( 'post_type' ) == 'my_custom_post_type' ) {
// This prevents other orderby options from breaking.
if ( !$query->get( 'orderby' ) ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment