Created
January 24, 2017 15:45
-
-
Save mklasen/f945388c4916c9853b9bb494d63134ae to your computer and use it in GitHub Desktop.
Order posts in archive page based on child terms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_filter( 'posts_clauses', function( $pieces, $obj ) { | |
| global $wpdb; | |
| $pieces['join'] .= " LEFT JOIN `$wpdb->term_relationships` AS trs ON ($wpdb->posts.ID = trs.object_id)"; | |
| $pieces['join'] .= " LEFT JOIN `$wpdb->term_taxonomy` AS tt ON (trs.term_taxonomy_id = tt.term_taxonomy_id)"; | |
| $pieces['join'] .= " LEFT JOIN `$wpdb->terms` AS t ON (tt.term_id = t.term_id)"; | |
| $pieces['join'] .= " LEFT JOIN `$wpdb->term_taxonomy` AS parent ON (parent.parent = trs.term_taxonomy_id)"; | |
| $pieces['join'] .= " LEFT JOIN `$wpdb->terms` AS parent_terms ON (parent.term_id = parent_terms.term_id)"; | |
| $pieces['where'] .= " AND (tt.taxonomy = '*YOUR_TAXONOMY*')"; | |
| $pieces['orderby'] = "IFNULL(parent_terms.slug, t.slug) ASC"; | |
| $pieces['limits'] = "LIMIT 0, 999"; | |
| return $pieces; | |
| }, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment