Home › Forums › ACF PRO › Order By Sub Field Value with SQL › Reply To: Order By Sub Field Value with SQL
The problem is that with the way you’re doing the query, which is really the only way to do it with repeater sub fields, I don’t thing that there’s a way to match up, for example results_0_technique
with results_0_score
.
I think that if you need to sort by the score field that you’re going to need to do that sorting using PHP.
You could loop through the rows as you’re doing and then assign the values to a multidimensional array, the you could build a php usort
function to sort the array.
This is just an example and it has not been tested.
// example array with a couple of the fields
$results = array();
foreach ($rows as $row) {
$results[] = array (
'technique ' => get_post_meta($row->post_id, $technique_meta_key, true)
'score ' => get_post_meta($row->post_id, $score_meta_key, true);
);
} // end foreach
usort($results, 'sort_results');
function sort_results($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
} elseif ($a['score'] < $b['score']) {
return -1;
} else {
return 1;
}
} // end function
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.