WP_Query orderby e tax_query

Meu objective é ter postagens de posts personalizadas (CPT para simplificar) a partir de uma taxonomia personalizada (CT por simplicidade), digamos CT Hometown com slug Nebraska para ser listado agrupado por outra CT, digamos as categorias de atividade. Eu consegui 50% da tarefa, e eu posso fazer os outros 50%, mas não juntos na mesma consulta.

Deixe ver o primeiro caso onde eu lista todas as postagens do meu CPT agrupadas pela CT da cidade natal

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $activities_args = array( 'post_type' => 'activities', 'paged' => $paged, 'posts_per_page' => '15', 'orderby' => 'taxonomy.activity_categories', 'ordertax' => 'ASC', 'order' => 'DESC' ); add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 ); $activities_query = new WP_Query( $activities_args ); remove_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 ); if ( $activities_query->have_posts() ) { echo '
    '; $current_taxonomy = ''; while ( $activities_query->have_posts() ) { $activities_query->the_post(); $first_activities_categories = ''; $terms = get_the_terms( get_the_ID(), 'activity_categories' ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { if (empty($first_activities_categories)) { $first_activities_categories = $term->name; $first_activities_categories_desc = $term->description; } } } if($current_taxonomy != $first_activities_categories) { if (!empty($current_taxonomy)) { echo '
'; } echo '
  • '.$first_activities_categories.'

    '.$first_activities_categories_desc.'

    '; echo '
      '; $current_taxonomy = $first_activities_categories; } echo '
    • '; the_title( '

      ', '

      ' ); the_excerpt(); echo '
    • '; } /* while ( have_posts() ) */ echo '
  • '; } /* if ( have_posts() ) */ show_pagination($activities_query);

    e em minhas funções.php

     function orderby_tax_clauses( $clauses, $wp_query ) { $orderby_arg = $wp_query->get('orderby'); if ( ! empty( $orderby_arg ) && substr_count( $orderby_arg, 'taxonomy.' ) ) { global $wpdb; $bytax = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC)"; $array = explode( ' ', $orderby_arg ); if ( ! isset( $array[1] ) ) { $array = array( $bytax, "{$wpdb->posts}.post_date" ); $taxonomy = str_replace( 'taxonomy.', '', $orderby_arg ); } else { foreach ( $array as $i => $t ) { if ( substr_count( $t, 'taxonomy.' ) ) { $taxonomy = str_replace( 'taxonomy.', '', $t ); $array[$i] = $bytax; } elseif ( $t === 'meta_value' || $t === 'meta_value_num' ) { $cast = ( $t === 'meta_value_num' ) ? 'SIGNED' : 'CHAR'; $array[$i] = "CAST( {$wpdb->postmeta}.meta_value AS {$cast} )"; } else { $array[$i] = "{$wpdb->posts}.{$t}"; } } } $order = strtoupper( $wp_query->get('order') ) === 'ASC' ? ' ASC' : ' DESC'; $ot = strtoupper( $wp_query->get('ordertax') ); $ordertax = $ot === 'DESC' || $ot === 'ASC' ? " $ot" : " $order"; $clauses['orderby'] = implode(', ', array_map( function($a) use ( $ordertax, $order ) { return ( strpos($a, 'GROUP_CONCAT') === 0 ) ? $a . $ordertax : $a . $order; }, $array ) ); $clauses['join'] .= " LEFT OUTER JOIN {$wpdb->term_relationships} as relationship "; $clauses['join'] .= "ON {$wpdb->posts}.ID = relationship.object_id"; $clauses['join'] .= " LEFT OUTER JOIN {$wpdb->term_taxonomy} "; $clauses['join'] .= "USING (term_taxonomy_id)"; $clauses['join'] .= " LEFT OUTER JOIN {$wpdb->terms} USING (term_id)"; $clauses['groupby'] = "object_id"; $clauses['where'] .= " AND (taxonomy = '{$taxonomy}' OR taxonomy IS NULL)"; } return $clauses; } 

    Esta parte funciona muito bem, mas isso me remove a posibildade de um imposto arq se eu adicionar:

     'tax_query' => array( array( 'taxonomy' => 'hometown', 'field' => 'slug', 'terms' => 'nebraska' ) ) 

    para meus $activities_args eu recebo esse erro

     WordPress database error: [Column 'term_taxonomy_id' in from clause is ambiguous] SELECT SQL_CALC_FOUND_ROWS wp_vnr_posts.ID FROM wp_vnr_posts INNER JOIN wp_vnr_term_relationships ON (wp_vnr_posts.ID = wp_vnr_term_relationships.object_id) LEFT OUTER JOIN wp_vnr_term_relationships as relationship ON wp_vnr_posts.ID = relationship.object_id LEFT OUTER JOIN wp_vnr_term_taxonomy USING (term_taxonomy_id) LEFT OUTER JOIN wp_vnr_terms USING (term_id) WHERE 1=1 AND ( wp_vnr_term_relationships.term_taxonomy_id IN (2) ) AND wp_vnr_posts.post_type = 'activities' AND (wp_vnr_posts.post_status = 'publish') AND (taxonomy = 'activity_categories' OR taxonomy IS NULL) GROUP BY object_id ORDER BY GROUP_CONCAT(wp_vnr_terms.name ORDER BY name ASC) ASC, wp_vnr_posts.post_date DESC LIMIT 0, 15 

    Obrigado em avançado para todos ajudando com esta questão!

    Solutions Collecting From Web of "WP_Query orderby e tax_query"