WordPress | Order post from term – > children custom taxonomy


i have a cpt: pro

Custom taxonomy: servizi_pro

where pro they are the professionals

and servizi_pro are the services they can do to customers.


John Dean

|_ website

I need to sort the ASC or DESC posts based on the prices against the service sought, where the service is inside the variable $art_p


$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

and foreach:

$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) {
        '<div class="numberCircle">%s€</div>',
        str_pad($price, 9)

but with this foreach I only order the prices outside the query and therefore I don’t have access to the post data, consequently it is not useful to me I want to sort the posts by prices instead

user14608849 2 months 0 Answers 19 views 0

Leave an answer