1

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!

1 Answer 1

1

It appeared it was due to the fact that WordPress doesn’t understand that you need to order by certain post meta unless you specify it.

So in my case adding this fixed the sorting issue:

add_action( 'pre_get_posts', 'custom_orderby' );
    function custom_orderby( $query ) {
        if( ! is_admin() )
            return;

        $orderby = $query->get( 'orderby');

        if( 'featured' == $orderby ) {
            $query->set('meta_key','featured');
            $query->set('orderby','meta_value');
        }
        elseif ( 'staff_order' == $orderby ) {
            $query->set('meta_key','staff_order');
            $query->set('orderby','meta_value_num');
        }
        elseif ( 'department' == $orderby ) {
            $query->set('meta_key','department');
            $query->set('orderby','meta_value');
        }
    }

Was able to fix the problem with the help of the member of Advanced Custom Fields community -

http://support.advancedcustomfields.com/forums/topic/sortin-custom-columns-with-custom-fields-sorts-only-by-date/

and by reading

http://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable--wp-25095

Not the answer you're looking for? Browse other questions tagged or ask your own question.