wordpress - orderby query by "type" is not working?

800 views Asked by At

i am trying to query a list in a specific post type order but it doesn't work.

$args = array(              
                'post_type' => array('a','b', 'c', 'd', 'e'),
                'tax_query' => array(
                        array(
                            'taxonomy' => 'category',
                            'field' => 'slug',
                            'terms' =>  ''.$term_list.''
                            ),
                        ),
                'orderby' => 'type'

                );

what's wrong? it works great with 'rand' and other query string but 'type' doesn't seems to be enough!

thanks!

2

There are 2 answers

9
certainstrings On

The only way I've got this working was to filter pre_get_posts. I haven't dug into core to really see what gets set, but the filter does work.

function my_search_filter( $query ) {

    if( $query->is_search() ) {
        $query->set( 'post_type', array('post', 'page','case-study','office', 'career', 'event') );
        $query->set( 'orderby', 'type');
    }
}
add_filter( 'pre_get_posts', 'my_search_filter' );
1
certainstrings On

It sounds like it's a single view of a custom post type. You could do something like this:

function my_single_filter( $query ) {

    if( $query->is_singular('custom_post_type_slug') ) {
        $query->set( 'post_type', array('a','b','c','d','e') );
        $query->set( 'orderby', array( 
        array( 'taxonomy' => 'category', 
               'field' => 'slug', 
               'terms' => ''.$term_list.'' ), 
        ));
        $query->set( 'orderby', 'type');
    }
}
add_filter( 'pre_get_posts', 'my_single_filter' );

This would go in your functions.php file. You will have to get the data via functions.php for the $term_list variable. In your single.php file you can place the default loop.