0

I order the product list by one custom field with this code:

add_action( 'woocommerce_product_query', 'imcar_custom_product_query' );
function imcar_custom_product_query( $query ) {

    if( ! $query->is_main_query() )
        return;

    $query->set( 'meta_key', 'ordern_d' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'ASC' );
}

But I need to order by two custom fields, It's possible? The other field name is order_ln.

Thanks

EDIT:

I'm just solve it

    add_action( 'woocommerce_product_query', 'imcar_custom_product_query' );
function imcar_custom_product_query( $query ) {

    if( ! $query->is_main_query() )
        return;

    $meta_query = array(
        array(
            'ordern_d_clause' => array(
                'key' => 'ordern_d',
                'compare' => 'EXISTS'
            )
        ),
        array(
            'orden_ln_clause' => array(
                'key' => 'orden_ln',
                'compare' => 'EXISTS'
            )
        ),
        array(
            'orden_r_clause' => array(
                'key' => 'orden_r',
                'compare' => 'EXISTS'
            )
        )
    );

    $query->set('meta_query', $meta_query);
    $query->set('orderby', array('ordern_d_clause' => 'ASC', 'orden_ln_clause' => 'ASC', 'orden_r_clause' => 'ASC'));

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.