'Wordpress sql, re-ordering array based on meta value

In my wordpress functions file, I'm getting sql query results of only post IDs, then taking those IDs to create an array that I send to my page.

This works perfectly but I want to re-order it based on one of the values I generate in the array process. I have a value of 'featured' and I want to make it so that any results where featured = 1 are first in the array.

$myresults = $wpdb->get_results( $sqlStatement );
$ress = array();
$cnt = 0; 
$imgpath = '';
            
    if(count($myresults)>0){
        foreach ($myresults as $key => $value) {
            $id = $value->ID;
            if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
                        
                        
            $ress[$cnt]['featured']  = get_post_meta($id,'_awpcp_is_featured',true);
                        
            $imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
              if($imgpath){
                    $ress[$cnt]['imgs'] = $imgpath;
              }else{
                $ress[$cnt]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
                }                       
                $cnt++;
            }
        }
    echo json_encode($ress);
    }

Is there a way to simply use conditions in order to put those values first in the array?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source