I’m displaying posts currently using the WP_Query, which doesn’t include the state taxonomy within the $args because I do not want the state to be within the query. But I do want to display the state. I’m displaying the post’s corresponding state taxonomy via get_the_term_list($post->ID, ‘state’). My task is how to use WordPress functions to display the posts alphabetically by the state taxonomy.

I'm thinking I can include the value from the corresponding state taxonomy and include it within a new array which will also include the WP_Query post data that I want to display as well. Then I can use PHP's usort() function to sort through the new array by state taxonomy.

I'm having trouble executing this properly.


Unfortunately, Wordpress doesn't have a built-in way to sort by taxonomy term. You do, though, have a couple of options.

  1. You can iterate through your taxonomy terms and run a separate wp_query for each as this post shows.

  2. You can directly manipulate the SQL query to achieve what you want. This article shows how. Look particularly at the last, Direct JOINs method.

Your Answer

