0

This code is to order by ACF fields in admin page. It actually filter by custom field but not ordering it.

the data inside the fields is string and not numeric, so i think orderby meta_value is correct. And i make sure that this key is not empty, i tried all these terms (desc,DESC,asc,ASC)

$query->query_vars['order']
function my_author_filter_results($query){
    global $pagenow;
    if ( $pagenow === 'edit.php' ) {
        if(isset($_GET['custom_field_order'])){
            $custom_field_order = sanitize_text_field($_GET['custom_field_order']);
            $order = sanitize_text_field($_GET['order']) ;
            if($custom_field_order){
                $query->query_vars['meta_key'] = $custom_field_order;
                $query->query_vars['orderby'] = 'meta_value';
                $query->query_vars['order'] =  strtoupper($order);
            }
        }
    }

}
add_action('pre_get_posts','my_author_filter_results');

PS: it works fine on my localhost

0

After searching i found the issue, another plugin called (Post type order) that uses drag and drop, ignored the sorting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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