Created
March 8, 2019 14:09
-
-
Save maxyudin/4932a5267c5708c0c4bd68fd4c034e3d to your computer and use it in GitHub Desktop.
[WordPress] Make post (page) list table custom column sortable
This file contains hidden or 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 | |
// https://wordpress.stackexchange.com/a/293403/11761 | |
// Make sure to change MY_POST_TYPE, MY_CUSTOM_COLUMN and MY_META_KEY to the actual values. | |
// Add your custom column. Unset the date and set it again to keep it in the last column. You can skip this step. | |
function my_manage_MY_POST_TYPE_columns( $columns ) | |
{ | |
// save date to the variable | |
$date = $columns['date']; | |
// unset the 'date' column | |
unset( $columns['date'] ); | |
// unset any column when necessary | |
// unset( $columns['comments'] ); | |
// add your column as new array element and give it table header text | |
$columns['MY_CUSTOM_COLUMN'] = __('Custom Column Header Text'); | |
$columns['date'] = $date; // set the 'date' column again, after the custom column | |
return $columns; | |
} | |
// Make your column sortable using manage_edit-{$post_type}_sortable_columns filter | |
function my_set_sortable_columns( $columns ) | |
{ | |
$columns['MY_CUSTOM_COLUMN'] = 'MY_CUSTOM_COLUMN'; | |
return $columns; | |
} | |
// Populate column cells | |
function my_populate_custom_columns( $column, $post_id ) | |
{ | |
switch ( $column ) { | |
case 'MY_CUSTOM_COLUMN': | |
echo get_post_meta($post_id, 'MY_META_KEY', true); | |
break; | |
case 'MAYBE_ANOTHER_CUSTOM_COLUMN': | |
// additional code | |
break; | |
} | |
} | |
// If you don't check meta_query for empty (non-existent) values, your column will show only the posts having (non-empty) meta value until it will be sorted by another by default or another column | |
function my_sort_custom_column_query( $query ) | |
{ | |
$orderby = $query->get( 'orderby' ); | |
if ( 'MY_CUSTOM_COLUMN' == $orderby ) { | |
$meta_query = array( | |
'relation' => 'OR', | |
array( | |
'key' => 'MY_META_KEY', | |
'compare' => 'NOT EXISTS', // see note above | |
), | |
array( | |
'key' => 'MY_META_KEY', | |
), | |
); | |
$query->set( 'meta_query', $meta_query ); | |
$query->set( 'orderby', 'meta_value' ); | |
} | |
} | |
// Apply filters and actions. Check if you are not on the front-end, on the right page and the right post type is chosen | |
global $pagenow; | |
if ( is_admin() && 'edit.php' == $pagenow && 'MY_POST_TYPE' == $_GET['post_type'] ) { | |
// manage colunms | |
add_filter( 'manage_MY_POST_TYPE_posts_columns', 'my_manage_MY_POST_TYPE_columns' ); | |
// make columns sortable | |
add_filter( 'manage_edit-MY_POST_TYPE_sortable_columns', 'my_set_sortable_columns' ); | |
// populate column cells | |
add_action( 'manage_MY_POST_TYPE_posts_custom_column', 'my_populate_custom_columns', 10, 2 ); | |
// set query to sort | |
add_action( 'pre_get_posts', 'my_sort_custom_column_query' ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment