Created
October 19, 2015 21:40
-
-
Save ChrisLTD/666deeadca1bcdc07fc2 to your computer and use it in GitHub Desktop.
Custom Column Sorting Example Wordpress Admin
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 | |
// * | |
// Show Alternate ID column in admin | |
// * | |
add_filter('manage_posts_columns', 'add_alternate_id_columns', 10, 2); | |
function add_alternate_id_columns($posts_columns, $post_type) | |
{ | |
if( $post_type == 'offer-download' ) { | |
$posts_columns['offer_download_id'] = 'Internal ID'; | |
} | |
return $posts_columns; | |
} | |
add_action('manage_posts_custom_column', 'display_alternate_id_columns', 10, 2); | |
function display_alternate_id_columns($column_name, $post_id) | |
{ | |
if ('offer_download_id' == $column_name) { | |
echo get_field('offer_download_id', $post_id); | |
} | |
} | |
function alternate_id_sortable( $columns ) { | |
$columns['offer_download_id'] = 'alternate_id'; | |
return $columns; | |
} | |
add_filter( 'manage_edit-offer-download_sortable_columns', 'alternate_id_sortable' ); | |
function alternate_id_orderby( $vars ) { | |
if ( isset( $vars['orderby'] ) && 'alternate_id' == $vars['orderby'] ) { | |
$vars = array_merge( $vars, array( | |
'meta_key' => 'offer_download_id', | |
'orderby' => 'meta_value_num' | |
) ); | |
} | |
return $vars; | |
} | |
add_filter( 'request', 'alternate_id_orderby' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment