Skip to content

Instantly share code, notes, and snippets.

@arelthia
Last active July 22, 2018 22:25
Show Gist options
  • Save arelthia/5bb741ee03dc4462f70ea5b15d7b4898 to your computer and use it in GitHub Desktop.
Save arelthia/5bb741ee03dc4462f70ea5b15d7b4898 to your computer and use it in GitHub Desktop.
Change order of custom post type on admin edit.php screen
/*
* Change order of custom post type on admin edit.php screen from name to date
* https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
*/
function ptc_admin_order( $wp_query ) {
if (is_admin()) {
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'ptc_clients' || $post_type == 'ptc_proposals' ) {
$wp_query->set('orderby', 'date');
}
}
}
add_filter('pre_get_posts', 'ptc_admin_order');
/*
* Change order of custom post type on admin edit.php screen from name to date
* https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
*/
function ptc_admin_order( $wp_query ) {
if (is_admin()) {
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'ptc_clients' || $post_type == 'ptc_proposals' ) {
$wp_query->set('orderby', 'date');
//If you set the order to desc the colun is not sortable
$wp_query->set('order', 'DESC');
}
}
}
add_filter('pre_get_posts', 'ptc_admin_order');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment