I have put together the following code to add custom columns to WordPress posts admin.
The columns appear with their associated custom fields but the sort of the last_name doesn't work as intended.
last_name is text and is the surname of employees which uses Advanced Custom Fields.
The sort looks like it's sorting by id and not by the first letter of the last_name. It also doesn't reverse sort DESC/ASC.
I hope you can all help, I'm still new to php and am looking for some kind assistance.
/*-------------------------------------------------------------------------------
Custom Columns
-------------------------------------------------------------------------------*/
function my_post_columns($columns)
{
$columns = array(
'cb' => '<input type="checkbox" />',
'thumb_image' => 'Thumb Image',
'title' => 'Title',
'second_name' => 'Second Name',
'author' => 'Author',
'date' => 'Date',
);
return $columns;
}
function my_custom_columns($column)
{
global $post;
if($column == 'thumb_image')
{
echo '<img width="76" height="99" src="'.get_field('thumb_image').'" />';
}
elseif($column == 'second_name')
{
echo get_field('second_name');
}
}
add_action("manage_posts_custom_column", "my_custom_columns");
add_filter("manage_edit-post_columns", "my_post_columns");
/*-------------------------------------------------------------------------------
Custom Sort of Second Name
-------------------------------------------------------------------------------*/
function my_column_register_sortable( $columns )
{
$columns['second_name'] = 'second_name';
return $columns;
}
add_filter("manage_edit-post_sortable_columns", "my_column_register_sortable" );
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire