Created
September 14, 2014 21:03
-
-
Save clifgriffin/405fdacb94412f9cfba3 to your computer and use it in GitHub Desktop.
Order custom post type archive by title.
This file contains 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
<?php | |
// Don't incldue opening PHP tag | |
add_filter( 'posts_orderby' , 'custom_cpt_order' ); | |
function custom_cpt_order( $orderby ) { | |
global $wpdb; | |
// Check if the query is for an archive | |
if ( is_archive() && get_query_var("post_type") == "my_custom_post_type" ) { | |
// Query was for archive, then set order | |
return "$wpdb->posts.post_title ASC"; | |
} | |
return $orderby; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This not always works. If you filter using tax_query a limited group of items (example 3 red cards out of 21 colored cards, taxonomy "color") you'll get 9 cards (number correct), but not all red since are ordered by post_title.
example, items:
expected result:
"B", "D"
result
"A", "B"