Home > other >  Wordpress sql, re-ordering array based on meta value
Wordpress sql, re-ordering array based on meta value

Time:05-24

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?

CodePudding user response:

Using a function get_post_meta in an array is bad practice. I would advise first querying the database to extract data from custom fields.

However, I think there are two ways to answer your question:

  1. Use 2 arrays and merge them together after iteration:
    $myresults = $wpdb->get_results( $sqlStatement );
    $ress1 = $ress2 = 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) != ''){
                $is_featured = get_post_meta($id,'_awpcp_is_featured',true);
                $imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
    
                $array_name = !empty($is_featured) ? 'ress1' : 'ress2';
    
                $$array_name[$cnt]['featured']  = $is_featured;
                if($imgpath){
                    $$array_name[$cnt]['imgs'] = $imgpath;
                }else{
                    $$array_name[$cnt]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
                }
                $cnt  ;
            }
        }
        $ress = array_merge($ress1, $ress2);
        echo json_encode($ress);
    }
  1. Use 2 counters:
    $myresults = $wpdb->get_results( $sqlStatement );
    $ress = array();
    $cnt1 = 0;
    $cnt2 = 1000000; 
    $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) != ''){


                $is_featured = get_post_meta($id,'_awpcp_is_featured',true);
                $imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');

                $counter_name = !empty($is_featured) ? 'cnt1' : 'cnt2';
                $ress[$$counter_name]['featured']  = get_post_meta($id,'_awpcp_is_featured',true);

                $imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
                if($imgpath){
                    $ress[$$counter_name]['imgs'] = $imgpath;
                }else{
                    $ress[$$counter_name]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
                }
                $$counter_name  ;
            }
        }
        $ress = array_values( $ress );
        echo json_encode($ress);
    }
  • Related