Last active
August 29, 2015 14:09
-
-
Save JiveDig/8824eb37d58284fe55ac to your computer and use it in GitHub Desktop.
Add custom admin sortable columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'manage_edit-resident_columns', 'set_custom_edit_resident_columns' ); | |
add_action( 'manage_resident_posts_custom_column' , 'custom_resident_column', 10, 2 ); | |
function set_custom_edit_resident_columns($columns) { | |
$columns = array( | |
'cb' => '<input type="checkbox" />', | |
'title' => 'Title', | |
'display' => 'Display', | |
); | |
return $columns; | |
} | |
function custom_resident_column( $column, $post_id ) { | |
if ( $column == 'display') { | |
if ( '1' == get_field('res_display', $post_id) ) { | |
echo 'Yes'; | |
} else { | |
echo 'No'; | |
} | |
} | |
} | |
//* Make column sortable | |
add_filter("manage_edit-resident_sortable_columns", "bhl_column_register_sortable" ); | |
function bhl_column_register_sortable( $columns ) | |
{ | |
$columns['display'] = 'display'; | |
return $columns; | |
} | |
//* Do the sorting | |
add_action( 'pre_get_posts', 'bhl_order_classes' ); | |
function bhl_order_classes( $query ) { | |
$post_type = $query->get('post_type'); | |
// If resident cpt | |
if ( $post_type == 'resident' ) { | |
// If set to short by res_display custom field | |
if ( $query->get( 'orderby' ) == 'display' ) { | |
$query->set('meta_key','res_display'); | |
$query->set('orderby','meta_value_num'); | |
} elseif ( $query->get( 'orderby' ) == '' ) { | |
$query->set( 'orderby', 'title' ); | |
} | |
/* Post Order: ASC / DESC */ | |
if( $query->get( 'order' ) == '' ){ | |
$query->set( 'order', 'ASC' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment