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');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.