Site icon Hip-Hop Website Design and Development

manage_edit-{post_type}_sortable_columns sorts, but wrong!

Good afternoon!

This is an issue regarding the WordPress backend and custom post types. I have spent all Friday searching and trying possible solutions and, while they helped others, in my case they’re not doing what I need.

Status quo

I have a custom post type called “anco_project” it uses very few WP given data-fields but more custom ones from the ACF addon including a field called “anco_project_year_from” and one called “anco_project_year_to”, both set up as a ‘true number’ format.

The goal:
Since those two fields contain years of a construction’s start and end, I want to have them sorted by the numbers given. 1998, 2006, 2012, 2016, 2016

Frontend: With following code I managed to sort the projects by their starting year without any problem.

    $args = array(
            'post_type' => 'anco_project',
            'posts_per_page' => -1,
            'meta_key' => 'anco_project_year_from',
            'orderby' => 'meta_value_num',
            'order' => 'ASC'
    );
    query_posts($args);

Backend: Here I followed several guides I can’t recall URLs anymore (my brain kinda got wiped over the weekend) but the ‘final’ solution from Friday looks like this.

    // Administration: Register columns as sortable
    function anco_project_manage_sortable_columns( $columns ) {
            $columns['anco_project_year_from'] = 'anco_project_year_from';
            $columns['anco_project_year_to'] = 'anco_project_year_to';
            return $columns;
    }
    add_filter( 'manage_edit-anco_project_sortable_columns', 'anco_project_manage_sortable_columns' );


    // Administration: Teach wordpress to make the column sortable
    function anco_project_year_column_orderby( $vars ) {
            if ( isset( $vars['orderby'] ) && 'anco_project_year_from' == $vars['orderby'] ) {
                    $vars = array_merge( $vars, array(
                            'meta_key' => 'anco_project_year_from',
                            'orderby' => 'meta_value_num'
                    ) );
            } else if ( isset( $vars['orderby'] ) && 'anco_project_year_to' == $vars['orderby'] ) {
                    $vars = array_merge( $vars, array(
                            'meta_key' => 'anco_project_year_to',
                            'orderby' => 'meta_value_num'
                    ) );
            }
            return $vars;
    }
    add_filter( 'request', 'anco_project_year_column_orderby' );

I rechecked all the hooks used and it looks as if it should work like this.

The problem

From the code above I get pretty filters at the headers and it actually does something when pressing them, but it’s not sorting by anything that would make sense.

an example

Posttype opened fresh in backend without any filters.
URL: /wp-admin/edit.php?post_type=anco_project

Posttype sorted by ‘from’.

URL: /wp-admin/edit.php?post_type=anco_project&orderby=anco_project_year_from&order=asc

When I sort by ‘anco_project_year_to’ or change the code to sort by the ‘location’ column it always ends up like the second image’s order. Reversing the order from ASC to DESC doesn’t change the ordering either.

Did anyone experience the same problem and has a solution for it?