Support

Account

Home Forums Front-end Issues wp_query Orderby multiple meta doesnt work Reply To: wp_query Orderby multiple meta doesnt work

  • Query clauses have a few issues that are not documented and you only find them by finding theme.

    Query clauses do not work well with nested meta queries. Honestly, I don’t remember if they work at all with nested meta queries.

    Query clauses 100% do not work with a nested query that has multiple meta keys associated with it.

    
    // order by true/false field, desc and then date field asc
    $meta_query = array(
        'relation' => 'AND',
        'qw1' => array(
            // it does not matter what value this field is
            // it is only used for ordering
            'key' => 'activi_datumperiode',
            'compare' => 'EXISTS'
        ),
        'qw2' => array(
          // note that ACF fields not stored as dates in DB
          // they are text values (or number)
          'key' => 'activi_datum',
          'value' => date('Ymd'),
          'compare' => '>='
        ),
    );
    
    $orderby = array('qw1' => 'DESC', 'qw2' => 'ASC');