Last active
August 29, 2015 13:57
-
-
Save ajithrn/9831035 to your computer and use it in GitHub Desktop.
Wordpress: custom admin columns sorting
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 | |
/** | |
* custom admin columns for custom-post-type , and sorting | |
* | |
* custom_post_type: enter cutom post type as function name | |
* custom-post-type: enter your custom post here | |
* custom_column: enter your custom colum id | |
* custom_column1: enter your custom colum id | |
* CustomColumnName: enter your custom column name | |
* custom_meta: enter custom meta | |
*/ | |
//add item in header | |
add_filter('manage_custom-post-type_posts_columns', 'custom_post_type_admin_column_head'); | |
function custom_post_type_admin_column_head( $defaults ) { | |
//add column names to the defaults array dupicate below line for more column | |
$defaults['custom_column'] = 'CustomColumnName'; | |
$defaults['custom_column1'] = 'CustomColumnName1'; | |
return $defaults; | |
} | |
//add column | |
add_action( 'manage_custom-post-type_posts_custom_column', 'custom_post_type_admin_columns', 10, 2 ); | |
function custom_post_type_admin_columns( $column_name, $post_id ) { | |
//add colums to admin duplicate the below if loop for more itemscolumns | |
if ($column_name == 'custom_column') { | |
$custom_column = get_post_meta( $post_id, 'custom_meta', true ); | |
echo date( _x( 'F d, Y', 'CustomColumnName', 'textdomain' ), strtotime( $custom_column ) ); //date | |
} | |
if ($column_name1 == 'custom_column') { | |
$custom_column1 = get_post_meta( $post_id, 'custom_meta', true ); | |
echo $custom_column1; //another value | |
} | |
} | |
//make colunm sortable | |
add_filter( 'manage_edit-custom-post-type_sortable_columns', 'custom_post_type_admin_column_sorting' ); | |
function custom_post_type_admin_column_sorting( $columns ) { | |
//add column names sortable add items to the columns array dupicate below line for more column | |
$columns['custom_column'] = 'custom_column'; | |
$columns['custom_column1'] = 'custom_column1'; | |
return $columns; | |
} | |
//make columns sortable duplicate the below filter if there is more custom columns | |
add_filter( 'request', 'custom_column_column_orderby' ); | |
function custom_column_column_orderby( $vars ) { | |
if ( isset( $vars['orderby'] ) && 'custom_column' == $vars['orderby'] ) { | |
$vars = array_merge( $vars, array( | |
'meta_key' => 'custom_meta', | |
'orderby' => 'meta_value' | |
) ); | |
} | |
return $vars; | |
} | |
add_filter( 'request', 'custom_column1_column_orderby' ); | |
function custom_column1_column_orderby( $vars ) { | |
if ( isset( $vars['orderby'] ) && 'custom_column' == $vars['orderby'] ) { | |
$vars = array_merge( $vars, array( | |
'meta_key' => 'custom_meta', | |
'orderby' => 'meta_value' | |
) ); | |
} | |
return $vars; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment