Skip to content

Instantly share code, notes, and snippets.

@ckaklamanos
Last active October 8, 2015 12:08
Show Gist options
  • Save ckaklamanos/a9d6a7d8caa655d5ac8c to your computer and use it in GitHub Desktop.
Save ckaklamanos/a9d6a7d8caa655d5ac8c to your computer and use it in GitHub Desktop.
Wordpress Custom Post Type: Add Order column in admin list page
add_filter('manage_$post_type_posts_columns', 'set_$post_type_columns');
function set_$post_type_columns($columns) {
$columns['order'] = 'Order';
return $columns;
}
add_action('manage_$post_type_posts_custom_column', 'set_$post_type_show_columns');
function set_$post_type_show_columns($name) {
global $post;
switch ($name) {
case 'order':
$views = $post->menu_order;
echo $views;
}
}
function set_$post_type_order_column_sortable($columns){
$columns['order'] = 'menu_order';
return $columns;
}
add_filter('manage_edit-$post_type_sortable_columns','set_$post_type_order_column_sortable');
/* Sort posts in wp_list_table by column in ascending or descending order. */
function set_disease_default_order($query){
/*
Set post types.
_builtin => true returns WordPress default post types.
_builtin => false returns custom registered post types.
*/
$post_types = get_post_types(array('_builtin' => false), 'names');
$post_type = $query->get('post_type');
if(in_array('disease', $post_types) && $post_type == 'disease'){
if($query->get('orderby') == ''){
$query->set('orderby', 'menu_order');
}
if($query->get('order') == ''){
$query->set('order', 'ASC');
}
}
}
if(is_admin()){
add_action('pre_get_posts', 'set_disease_default_order');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment