Last active
October 18, 2017 13:57
-
-
Save hereswhatidid/510c5e191f578325966ded172b513938 to your computer and use it in GitHub Desktop.
Add a custom, sortable column to a WordPress post type admin table
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 | |
class HWID_Post_Sortable { | |
static $post_type_slug = 'post'; | |
static function init() { | |
add_action( 'manage_' . self::$post_type_slug . '_posts_custom_column', array( | |
'HWID_Post_Sortable', | |
'sort_column_display', | |
), 10, 2 ); | |
add_filter( 'manage_' . self::$post_type_slug . '_posts_columns', array( | |
'HWID_Post_Sortable', | |
'custom_columns', | |
) ); | |
add_filter( 'manage_edit-' . self::$post_type_slug . '_sortable_columns', array( | |
'HWID_Post_Sortable', | |
'custom_sortable_column', | |
) ); | |
add_action( 'pre_get_posts', array( | |
'HWID_Post_Sortable', | |
'custom_sort_order', | |
) ); | |
} | |
/** | |
* Populate the display value of the Display Order column in the admin table | |
* | |
* @param string $column | |
* @param string|int $post_id | |
*/ | |
static function sort_column_display( $column, $post_id ) { | |
switch ( $column ) { | |
case 'display_order': | |
if ( ! $display_order = get_post_meta( $post_id, 'display_order', true ) ) { | |
$display_order = 9999; | |
} | |
echo $display_order; | |
break; | |
} | |
} | |
/** | |
* Make the display order column sortable | |
* | |
* @param array $columns | |
* | |
* @return array | |
*/ | |
static function custom_sortable_column( $columns ) { | |
$columns['display_order'] = 'display_order'; | |
return $columns; | |
} | |
/** | |
* Add Display Order to the custom columns | |
* | |
* @param array $columns | |
* | |
* @return array | |
*/ | |
static function custom_columns( $columns ) { | |
return self::add_column_array( $columns, array( | |
'display_order' => __( 'Display Order', 'hwid' ), | |
) ); | |
} | |
/** | |
* Sort the admin columns by display order if specified | |
* | |
* @param \WP_Query $wp_query | |
*/ | |
static function custom_sort_order( $wp_query ) { | |
if ( ! is_admin() ) { | |
return; | |
} | |
if ( ! $wp_query->is_main_query() ) { | |
return; | |
} | |
if ( ! $wp_query->get('post_type' ) === self::$post_type_slug ) { | |
return; | |
} | |
if ( ! $orderby = $wp_query->get( 'orderby' ) ) { | |
return; | |
} | |
switch ( $orderby ) { | |
case 'display_order': | |
$wp_query->set( 'orderby', 'meta_value_num' ); | |
$wp_query->set( 'meta_key', 'display_order' ); | |
$wp_query->set( 'order', $wp_query->get( 'order' ) ); | |
break; | |
} | |
} | |
/** | |
* Add a new admin column to the second to last position | |
* | |
* @param array $columns - Existing columns | |
* @param array $new_column - New column to add | |
* | |
* @return array | |
*/ | |
static function add_column_array( $columns, $new_column ) { | |
$first_chunk = array_slice( $columns, 0, count( $columns ) - 1 ); | |
$second_chunk = array_slice( $columns, -1 ); | |
return array_merge( $first_chunk, $new_column, $second_chunk ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment