• Hello,
    I can’t do a multiple sort with the post date field and a custom field.
    If I use only one field, the sort is performed

    query
    ===============
    add_action( ‘elementor/query/q_img-copertina-max’, function( $query ) {
    $meta_query = [

    [

    ‘key’ => ‘visibile’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’,
    ] ,
    [
    ‘key’ => ‘vis_cop_cat’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’
    ],

    ];

    $query->set(‘meta_query’, $meta_query);
    $query->set(‘meta_key’, ‘ordinamento’);
    $query->set( ‘orderby’, array(
    ‘post_date’ => ‘desc’,
    ‘ordinamento’ => ‘asc’,

    ));

    thanks for attention

Viewing 2 replies - 1 through 2 (of 2 total)
  • HI
    If you wish to order by two different pieces of postmeta (for example, City first and State second), you need to combine and link your meta query to your orderby array using ‘named meta queries’. See the example below:

    $q = new WP_Query( array(
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    ‘state_clause’ => array(
    ‘key’ => ‘state’,
    ‘value’ => ‘Wisconsin’,
    ),
    ‘city_clause’ => array(
    ‘key’ => ‘city’,
    ‘compare’ => ‘EXISTS’,
    ),
    ),
    ‘orderby’ => array(
    ‘city_clause’ => ‘ASC’,
    ‘state_clause’ => ‘DESC’,
    ),
    ) );

    Ref : Ref and ctrl+f5 ‘orderby’ with multiple ‘meta_key’s

    Thread Starter maxcondor

    (@maxcondor)

    I rewrote the query.
    But it does not work,
    it doesn’t return any records to me

    add_action( ‘elementor/query/q_img-copertina-max’, function( $query ) {
    $meta_query = [
    ‘relation’ => ‘AND’,
    [

    ‘key’ => ‘visibile’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’,
    ] ,
    [
    ‘key’ => ‘vis_cop_cat’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’
    ],
    ‘n_ordinamento’=> [
    ‘key’ => ‘ordinamento’,
    ‘compare’ => ‘EXISTS’
    ],
    ‘n_date’=> [
    ‘key’ => ‘post_date’,
    ‘compare’ => ‘EXISTS’
    ]

    ];

    $query->set(‘meta_query’, $meta_query);
    $query->set( ‘orderby’, array(
    ‘n_ordinamento’ => ‘DESC’,
    ‘n_date’ => ‘DESC’,

    ));

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple orderby on query’ is closed to new replies.