Last active
October 27, 2016 08:55
-
-
Save isuke01/a1da12833b74f2e0d3bd48351b9be475 to your computer and use it in GitHub Desktop.
Wordpress custom post type custom sorting column on backend
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
/* Sorting custom columns */ | |
// Add new column in view | |
// quiz_answer - post-type | |
add_filter('manage_quiz_answer_posts_columns', 'tankenbak_quiz_answer_table_head'); | |
function tankenbak_quiz_answer_table_head( $defaults ) { | |
$defaults['answers_points'] = __('Points', 'tankenbak'); | |
//$defaults['ticket_status'] = __('Status', 'tankenbak') | |
return $defaults; | |
} | |
// Add value to column | |
add_action( 'manage_quiz_answer_posts_custom_column', 'tankenbak_quiz_answer_table_content', 10, 2 ); | |
function tankenbak_quiz_answer_table_content( $column_name, $post_id ) { | |
if ($column_name == 'answers_points') { | |
$votes_count = get_post_meta( $post_id, 'answers_points', true ); | |
echo $votes_count; | |
} | |
} | |
//add sorting option | |
add_filter( 'manage_edit-quiz_answer_sortable_columns', 'tankenbak_quiz_answer_table_sorting' ); | |
function tankenbak_quiz_answer_table_sorting( $columns ) { | |
$columns['answers_points'] = 'answers_points'; | |
return $columns; | |
} | |
// edit sorting to work on numbers | |
add_filter( 'request', 'tankenbak_quiz_answer_column_orderby' ); | |
function tankenbak_quiz_answer_column_orderby( $vars ) { | |
if ( isset( $vars['orderby'] ) && 'answers_points' == $vars['orderby'] ) { | |
$vars = array_merge( $vars, array( | |
'orderby' => 'meta_value_num', | |
'meta_key' => 'answers_points', | |
) ); | |
} | |
return $vars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment