Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to sort posts by custom taxonomy (shoe_color) ASC|DESC? I would like to do this using query_posts and passing the orderby=shoe_color into it.

Here is the code I'm trying to work out:

function shoe_color_orderby( $wp_query ) {
    global $wpdb;
    return "(
        SELECT GROUP_CONCAT(slug ORDER BY slug ASC)
        FROM $wpdb->term_relationships
        INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id)
        INNER JOIN $wpdb->terms USING (term_id)
        WHERE $wpdb->posts.ID = object_id
        AND taxonomy = 'shoe_color'
        GROUP BY object_id
    ) ";
}
add_filter( 'posts_orderby', 'shoe_color_orderby');
share|improve this question
    
Check this search queries: posts_orderby and pre_get_posts –  brasofilo Jul 10 '13 at 23:21
    
I guess this is what you are looking for: wordpress.stackexchange.com/questions/14306/… –  rfrq Jul 11 '13 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.