Support

Account

Home Forums General Issues Can’t sort with an ACF field? Reply To: Can’t sort with an ACF field?

  • With a lot of research and testing, I believe that it is indeed impossible to do so with an ACF field. you must therefore modify it via a function in functions.php that I wrote and that I share with you

    function custom_order_by_acf_field_query($query) {
    
        if ( !is_admin()) { 
    		if(isset($query->query_vars['orderby']) && $query->query_vars['orderby'] == '{{coefficient}}') {
                $query->set('meta_key', 'coefficient');
                $query->set('orderby', 'meta_value_num');
                $query->set('order', 'DESC');
    		}
    
        }
    }
    
    add_action('pre_get_posts', 'custom_order_by_acf_field_query');