Sort the child taxonomies by number

Question

I explain the structure not to be long but because I am faced with a case that I do not know how to solve.

my goal is to sort the posts in ascending or descending order with respect to the service requested by the user, sorting the child taxonomies which are treated as prices by number

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 €

On the user side, he can specify the service he requires and this is stored inside the variable: $art_p

Now the problems begin, because, if I use a query to show me what I have transcribed above:

$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
                 ORDER BY  {$wpdb->prefix}terms.name ASC
                 
                 ")
);

if I obtain the information with a foreach, having explained that the prices of the services of professionals are treated as children of custom taxonomies

I get this:

When you try to sort an array that contains a single element, it doesn’t matter if you call it sort () or rsort () because the lone element has nowhere to move.
Since you are ignoring the relationship between a child price and $ r-> ID, you can simply accumulate all the child prices into a main array in the inner loop, then sort and view the results when the parent loop is finished.

$prices = [];
foreach ($qprice as $r) {
    $child_terms = get_term_children($r->term_id, 'servizi_pro');
    foreach (wp_get_post_terms($r->ID, 'servizi_pro') as $term) {
        if (in_array($term->term_id, $child_terms)) {
            $prices[] = $term->name;
        }
    }
}
sort($prices, SORT_NUMERIC);
foreach ($prices as $price) {
    printf(
        '<div class="numberCircle">%s€</div>',
        str_pad($price, 9)
    );
}

I get the prices in ascending order by setting sort or rsort, but I have no access to the query foreach, so I cannot access the post values, consequently in a visual structure like booking: where the structures come out in ascending or descending order I here I only have access to all children (price) of the custom taxonomy: servizi_pro, compared to the service requested in $ art_p

instead my goal is to order:

post: pro

taxonomy: servizi_pro

service: $art_p

in ascending or descending order of the child taxonomies of servizi_pro


Professional: Name Surname

**Taxonomy main:** servizi_pro
|
|_Taxonomy : Website
  |
  |_ Child 1600 €
0
user14608849 2 months 0 Answers 15 views 0

Leave an answer