PHP | sort correct code but unfiltred result

Question

I explain the structure not to elaborate but since I am faced with an unusual case as the code is correct but I cannot filter my results through the sort command, I prefer to explain in a way that maybe someone can come up with the cause.

I am creating a portal of Professionals where these last ones are treated as custom post type: pro.

Professionals specify a series of services, these services are treated as custom taxonomies: servizi_pro

so we are up to now in case:

Professional: Name Surname

Service: Website

Now to indicate the prices of these services, I have always used custom taxonomies but setting them as children

Therefore:

Professional: Name Surname

Service: Website
|
| _ 1600 €

To show the Professionals who, for example, run websites, I write it in this way: website is contained within the variable: $art

code:

$art_p = $_GET['art'];      // Servizio

global $wpdb;

    $qprice = $wpdb->get_results(
        $wpdb->prepare(" SELECT *
                     FROM {$wpdb->prefix}posts 
                                         
                     LEFT JOIN {$wpdb->prefix}post_views
                     ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}post_views.id 
                     
                     LEFT JOIN {$wpdb->prefix}postmeta
                     ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}postmeta.post_id
                                         
                     LEFT JOIN {$wpdb->prefix}term_relationships 
                     ON {$wpdb->prefix}posts.ID =  {$wpdb->prefix}term_relationships.object_id 
                                         
                     LEFT JOIN {$wpdb->prefix}terms
                     ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}terms.term_id


                     WHERE {$wpdb->prefix}posts.post_type = 'pro'
                     AND {$wpdb->prefix}post_views.type = '4'  
                     AND {$wpdb->prefix}terms.name = '$art_p'

                                         
                     GROUP BY {$wpdb->prefix}posts.ID
                     
                     ")
    );

to retrieve and display the results on the screen by setting the decreasing prices, therefore from the most expensive to the least expensive, I do as follows:

foreach ($qprice as $r)
    {
        $children = $r->term_id;
        $child_terms = get_term_children($r->term_id, 'servizi_pro');
        $all_terms   = wp_get_post_terms($r->ID, 'servizi_pro');

        $final_price = [];
        foreach ( $all_terms as $term )
        {

            if (!in_array($term->term_id, $child_terms))
                continue;

            $price = $term->name;
            $final_price[] = $price;
        }

        rsort($final_price, SORT_NUMERIC);
        foreach($final_price as $pr_f) {
            echo '<span  class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
        }
    }

and everything works correctly.

The problem I have when I want to filter by sort instead, so from the cheapest price to the most expensive but if I change the piece of code written above

rsort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
    echo '<span  class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
}

to

sort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
    echo '<span  class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
}

it doesn’t work, why?

0
user14608849 2 months 0 Answers 13 views 0

Leave an answer