Skip to content

Instantly share code, notes, and snippets.

@JustinSainton
Created January 21, 2016 05:02
Show Gist options
  • Save JustinSainton/c29d3fb9a31bc036351f to your computer and use it in GitHub Desktop.
Save JustinSainton/c29d3fb9a31bc036351f to your computer and use it in GitHub Desktop.
Pouring a Drink for Everyone Who Has Had to Order By Term Slug
<?php
add_action( 'pre_get_posts', function( $query ) {
if ( ( is_post_type_archive( 'special-cpt' ) || ( is_search() && 'special-cpt' == $query->get( 'post_type' ) ) ) && $query->is_main_query() ) {
$query->set( 'orderby' , 'title' );
$query->set( 'order' , 'ASC' );
$query->set( 'tax_query', array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => array( 'uno', 'dos', 'tres' ),
),
) );
add_filter( 'posts_join', function( $join, $query ) {
global $wpdb;
return $join . ' INNER JOIN '. $wpdb->terms .' ON ( '. $wpdb->terms . '.term_id = ' . $wpdb->term_relationships . '.term_taxonomy_id )';
}, 10, 2 );
add_filter( 'posts_orderby', function( $join, $query ) {
global $wpdb;
return $wpdb->terms . '.slug,' . $wpdb->posts . '.post_title';
}, 10, 2 );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment