Create Post Query to order by a Taxonomy custom meta field


I have a standard WP_Query to search for Posts of a certain Custom Post Type. I am trying to extend this by filtering by a specific custom meta field that was set for a Taxonomy that is attached to the Post.

What I’ve done is created a Taxonomy, and in that Tax I’ve added a custom meta field called “sorting_weight” that is a numerical field. So when admin assign a CPT to this Tax, I would be able to query all CPT entries and then orderly that numerical field.

All the tutorials online are getting confused and thinking the user wants to sort by normal post custom meta. This is not what I want – I want to sort by the CPT’s Taxonomy’s custom meta.

From what I can gather, I can’t even pipe this logic into a normal WP_Query argument? If so, how would I refactor my code to order by this Taxonomy custom meta field “sorting_weight”?

The below shows the general query I am using:

     /**      * @return array      */     protected function returnQuery(): array     {         $  query = [             'post_type' => $  this->getPostTypeName(),             'show_posts' => -1,             'hide_empty' =>  true,             'meta_key' => 'year',             'orderby' => 'meta_value',             'order' => 'DESC'         ];          /**          * CPT Meta Queries          */         if ($  this->getSearchQuery()['appGallerySearch_Category']) {             $  query['meta_query'][] = [                 'key' => 'topic',                 'value' => $  this->getSearchQuery()['appGallerySearch_Category'],                 'compare' => 'LIKE'             ];         }         if ($  this->getSearchQuery()['appGallerySearch_Program']) {             $  query['meta_query'][] = [                 'key' => 'program',                 'value' => $  this->getSearchQuery()['appGallerySearch_Program'],                 'compare' => 'LIKE'             ];         }         if ($  this->getSearchQuery()['appGallerySearch_Division']) {             $  query['meta_query'][] = [                 'key' => 'divisions',                 'value' => $  this->getSearchQuery()['appGallerySearch_Division'],                 'compare' => 'LIKE'             ];         }         if ($  this->getSearchQuery()['appGallerySearch_Year']) {             $  query['meta_query'][] = [                 'key' => 'year',                 'value' => $  this->getSearchQuery()['appGallerySearch_Year'],                 'compare' => 'LIKE'             ];         }         if ($  this->getSearchQuery()['appGallerySearch_Location']) {             $  query['meta_query'][] = [                 'key' => 'region',                 'value' => $  this->getSearchQuery()['appGallerySearch_Location'],                 'compare' => 'LIKE'             ];         }         if ($  this->getSearchQuery()['appGallerySearch_Keyword']) {             $  query['meta_query'][] = [                 'key' => 'description',                 'value' => $  this->getSearchQuery()['appGallerySearch_Keyword'],                 'compare' => 'LIKE'             ];         }          return $  query;     }