• Hello,

    I was trying to filter post items by column, that uses a numeric ACF field and found a bug. Filtering by numeric field caused 10 appearing before 2, 31 before 4 etc.

    I’ve made a change in acf_admin_columns.php when setting:

    $query->set('orderby', 'meta_value');
    to:

    if($query->query['orderby'] == 'acf_nights_hidden') {
         $query->set('orderby', 'meta_value_num');
     } else {
         $query->set('orderby', 'meta_value');
     }

    where nights_hidden is my acf numeric field name. It helps in my case but I think it could be great if you can fix it generally :).

    Best,
    Sebastian.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey kopiczkos,
    hm…interesting issue and good idea…I will look into integrating this within one of the next versions…

    Thanks and best regards,
    Florian

    Hallo Florian,
    i had a similar Problem with Sorting. I have set the fix from @kopiczkos outside of the Plugin. The advantage is, that the Plugin is already updateable and the changes will not be overwriten on an Update.

    I post the code here. Maybe someone will find it helpful:

    
    function event_admin_column_sort($query)
    {
        $acf_active = (function_exists('acf_get_field_groups') && function_exists('acf_get_fields'));
        $acf_columns_active = class_exists('FleiACFAdminColumns');
        $is_valid_admin_screen = (bool) false;
        if (function_exists('get_current_screen') && $screen = get_current_screen()) {
            $is_valid_admin_screen = ($screen->base == 'edit' && $screen->post_type) || ($screen->base == 'edit-tags' && $screen->taxonomy); 
        }
        
        if (is_admin() && $acf_columns_active  && $acf_active && $is_valid_admin_screen && $query->query_vars && isset($query->query_vars['orderby'])) {
            if($query->query['orderby'] == 'acf_eventon_field_related_course') {
                $query->set('orderby', 'meta_value_num');
            } else {
                $query->set('orderby', 'meta_value');
            }
        }
        
        return $query;
    }
    
    add_action('pre_get_posts', 'event_admin_column_sort', 999);
    

    Another suggestion could be, that it could be helpful to set Class-Methods is_acf_active and is_valid_admin_screen to be public to use it outside the plugin.

    And a great feature would be, that the user can set on a selection field, if it should be sorted by value or by title of the saved option.

    But at last: Great Plugin! Many Thanks.

    @flei

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filter by numeric field’ is closed to new replies.