Is it possible to sort in wordpress by several meta fields? You need to do this sorting:

1 shows posts for which the recommend field is nonzero (the higher the value, the higher they are in the list).

2 posts are displayed on the rating field.

3 posts are shown in which and recommend = 0 and rating = 0 (sorting alphabetically).

It turns out that only one field is sorted, if the second is specified, nothing changes ...

Code:

$args = array( 'post_type' => 'companies', 'location' => $term, 'meta_query' => array( 'relation' => 'OR', 'recommend' => array( 'key' => 'recommend', ), 'rating' => array( 'key' => 'rating', ), ), 'orderby' => array( 'recommend' => 'DESC', 'rating' => 'DESC', 'meta_value_num' => 'DESC', 'title' => 'ASC' ), 'paged' => get_query_var('paged'), ); 

Although the field recommend and specify the first, according to it still does not sort. First, posts with sorting by rating are displayed, then alphabetically, the recommend field is not considered for some reason.

    1 answer 1

    It seems the problem was solved, just changed the 'relation' => 'OR', to 'relation' => 'AND' and began to sort as needed.