2

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

enter image description here

Posttype sorted by 'from'.

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

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?

2 Answers 2

0

Remove this:

// 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' );

and add this:

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

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

    if( 'anco_project_year_from' == $orderby ) {
        $query->set('meta_key','anco_project_year_from');
        $query->set('orderby','meta_value_num');
    } elseif('anco_project_year_to' == $orderby) {
        $query->set('meta_key','anco_project_year_to');
        $query->set('orderby','meta_value_num');
    }
}
1
  • Interesting idea. Unfortunately this does not change anything when trying to sort by the column. The order stays as it is.
    – lastria
    Commented Sep 27, 2016 at 6:16
0

This works for me. I have done it only for anco_project_year_from but you can easily add the condition for anco_project_year_to.

Hope it helps.

function anco_project_year_column_orderby( $query ) {
    if( ! is_admin() ) { 
       return;
    }

   $orderby = $query->get( 'orderby');
   if ( 'anco_project_year_from' == $orderby ) {

       $query->set('meta_key',$orderby);
       $query->set('orderby','meta_value_num');
   }
}

add_filter( 'pre_get_posts','anco_project_year_column_orderby');
4
  • This answer does not look more informative than the one already given.
    – nmr
    Commented Jan 11, 2020 at 16:43
  • @nmr respectfully, I tried the solution already given first, which for me did not work, much like OP. By looking around, I came up with the code above, which is sligthly different from the one proposed, and works for me.
    – Dave White
    Commented Jan 11, 2020 at 17:18
  • I see no reason why the presence of elsif in pre_get_posts filter would cause incorrect sorting. I added two number fields (using ACF), additional sortable columns and used my_slice_orderby() code. Sorting on both columns worked correctly, I did not notice that the years were in the wrong order. The code as written (in both cases) may not display posts without entered "from/to" year but it is quite another matter.
    – nmr
    Commented Jan 11, 2020 at 22:47
  • Since you say that sorting has started to work after the second condition has been removed, I will not argue. However, I would look for a problem in another piece of code. By the way, downvote was not from me.
    – nmr
    Commented Jan 11, 2020 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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