Ordering post by taxnomy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
add_filter('posts_clauses', 'posts_clauses_with_tax', 10, 2); function posts_clauses_with_tax( $clauses, $wp_query ) { global $wpdb; //array of sortable taxonomies $taxonomies = array('example-taxonomy', 'other-taxonomy'); if (isset($wp_query->query['orderby']) && in_array($wp_query->query['orderby'], $taxonomies)) { $clauses['join'] .= " LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id LEFT OUTER JOIN {$wpdb->terms} USING (term_id) "; $clauses['where'] .= " AND (taxonomy = '{$wp_query->query['orderby']}' OR taxonomy IS NULL)"; $clauses['groupby'] = "rel2.object_id"; $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) "; $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC'; } return $clauses; } |
The wordpress post query functions have many excellent features built in for selecting and ordering a post query, but the one thing it can’t do is order by taxonomy name. For the average user, this is not a big deal, but once you start making complicated relationships with custom post types and taxonomies, it[…]