I have a custom post type – “members”. With help of Advanced Custom Fields(AFC) I’ve created a Field Group with 5 fields and assigned that group to my “members” post type.
I followed this article to create custom admin columns for “members” – http://www.elliotcondon.com/advanced-custom-fields-admin-custom-columns/
I’ve created columns:
function members_columns($columns){
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => 'Name',
'picture' => 'Picture',
'department'=> 'Department',
'sex' => 'Sex',
'featured' => 'Fixed',
'staff_order'=> 'Order',
'date' => 'Date',
);
return $columns;
}
Populated them with data:
function members_custom_columns($column){
global $post;
if($column == 'picture'){
echo wp_get_attachment_image( get_field('picture', $post->ID), array(50,50) );
}
elseif($column == 'featured'){
if(get_field('featured')){
echo 'Yes';
} else {
echo '---';
}
}
elseif($column == 'staff_order'){
echo get_field('staff_order');
}
elseif($column == 'department'){
echo get_field('department');
}
elseif($column == 'sex'){
echo get_field('sex');
}
}
and made them sortable:
function members_column_register_sortable( $columns ){
$columns['featured'] = 'featured';
$columns['staff_order'] = 'staff_order';
$columns['department'] = 'department';
return $columns;
}
And included hooks in my plugin file:
add_action("manage_posts_custom_column", "members_custom_columns");
add_filter("manage_edit-members_columns", "members_columns");
add_filter("manage_edit-members_sortable_columns", members_column_register_sortable" );
The corresponding fields types in this field group are:
‘featured’ – True / False
‘staff_order’ – Number
‘department’ – Select
Result:
Visually it looks like it should: all the data correctly populating the columns including the pictures, required columns appears to be sortable, and when I click on headers some sorting appears.
The problem:
For some reason, sorting works correctly only for original ‘title’ and ‘date’ fields. Whenever I sort by any of the 3 added columns (‘featured’, ‘staff_order’ or ‘department’), it sort them in wrong order.
After experimenting and testing, it became apparent that each time any of these 3 custom columns were sorted by the order they were published, i.e. by ‘date’.
One more thing to notice, despite sorting in wrong order, the url parameters seemed to be correct each time, for example if I sort by ‘department’ the url is “/edit.php?post_type=members&orderby=department&order=asc”, yet it sorts by ‘date’.
When I run same query on my wp_postmeta to see if data returns in correct order, everything is ok.
Please help!