Home › Forums › General Issues › Order by ACF
I am having issues with ordering my posts by my ACF Star Rating. Does anyone know what I am doing wrong?
$args = array(
'taxonomy' => 'club',
'hide_empty' => '0',
'meta_key' => 'star_rating',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
echo $meta_key;
$terms = get_terms('club', $args);
You can’t order terms by a custom field.
orderby for get_terms() only accepts one of the argument listed on the codex page https://codex.wordpress.org/Function_Reference/get_terms
In order to do this you need to get the terms, then loop through the terms, get your custom field and add it to each term object and then build a PHP function to order them.
I’ve done this a few times
function my_sort_function($a, $b) {
if ($a->star_rating == $b->star_rating) {
return 0;
} elseif ($a->star_rating < $b->star_rating) {
return -1;
} else {
return 1;
}
}
$terms = get_terms($taxonomy, $args);
if ($terms) {
foreach ($terms as $index => $term) {
$terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
}
usort($terms, 'my_sort_function');
}
Thank you John,
As a beginner I am still not able to get it working this way. This is the code I have:
In my functions.php file:
function my_sort_function($a, $b) {
if ($a->star_rating == $b->star_rating) {
return 0;
} elseif ($a->star_rating < $b->star_rating) {
return -1;
} else {
return 1;
}
}
On my home page:
<?php
$args = array(
'taxonomy' => 'sock-club',
'hide_empty' => '0'
);
$terms = get_terms('sock-club', $args);
if ($terms) {
foreach ($terms as $index => $term) {
$terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
}
usort($terms, 'my_sort_function');
}
?>
Can you see what is wrong with my code?
<?php
$args = array(
'taxonomy' => 'sock-club',
'hide_empty' => '0'
);
$terms = get_terms('sock-club', $args);
if ($terms) {
foreach ($terms as $index => $term) {
$terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
}
usort($terms, 'my_sort_function');
// create the list after the terms are sorted
// need to loop through them again
$term_list = '';
foreach ($terms as $term) {
$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
}
}
?>
I wanted to note that in the above code “ussort” should be “usort”. I just spent 30 minutes trying to figure out why I couldn’t get this code to work until I finally noticed the typo.
sorry about that, I posted it wrong once and then it was copied over several times and wrong every time.
I’ve corrected the type so hopefully know one else will trip over the same thing.
You must be logged in to reply to this topic.
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!
ACF’s Repeater field is a versatile solution for repeating content in #WordPress.
— Advanced Custom Fields (@wp_acf) December 29, 2022
What’s your favorite use case for the Repeater field?
What’s the weirdest thing you’ve ever used it for?
Let us know!👇
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.