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