wordpress – orderby query by “type” is not working?

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!

Read More

thanks!

Related posts

Leave a Reply

2 comments

  1. 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' );
    
  2. 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.