• I’m using pre_get_posts to successfully output a list of CPTs using order as primary key:

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    
        if ( !is_admin() && $query->is_home() &&  $query->is_main_query() || is_feed() ) {
            $query->set('post_type', array( 'post', 'statsbox', 'imagebox', 'socialbox', 'quotebox', 'linkbox', 'videobox', 'textbox' ) );
            $query->set('orderby', 'meta_value_num');
            $query->set('meta_key', 'order');
            $query->set('order', 'ASC');
        }
    }

    I’m trying to add another meta_key client_name as primary sort key.

    Novice, I’ve tried:

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    
        if ( !is_admin() && $query->is_home() &&  $query->is_main_query() || is_feed() ) {
            $query->set('post_type', array( 'post', 'statsbox', 'imagebox', 'socialbox', 'quotebox', 'linkbox', 'videobox', 'textbox' ) );
            $query->set('orderby', 'meta_value meta_value_num');
            $query->set('meta_key', 'client_name order');
            $query->set('order', 'ASC ASC');
        }
    }

    Any help appreciated!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot stack parameters for ‘orderby’ as you have done. You have to pick one and go with it. If you have more complex needs you can use the ‘posts_orderby’ filter to directly alter the SQL query string. Of course, your needs must be able to be formed into a valid SQL ORDER BY clause.

    The filter will be applied to all queries, so you must either place a conditional so the change is selectively applied, or selectively add and remove the filter callback itself as needed.

Viewing 1 replies (of 1 total)
  • The topic ‘wp-query multiple orderby keys’ is closed to new replies.