I want to order the posts by 2 custom fields in the same call. How is this possible?
This next code order successfully, but only by only 1 NUMBER custom field (not STRING):
add_action('pre_get_posts', function ($q) {
if (
!is_admin() // Target only front end queries
&& $q->is_main_query() // Target the main query only
&& ($q->is_search() || $q->is_post_type_archive('data-base'))
) {
$q->set('meta_key', 'custom_field_1');
$q->set('order', 'DESC');
$q->set('orderby', 'meta_value');
}
});
Update 1:
Currently @Mohammed Yassine CHABLI's first answer works, but it doesn't sort by number
, but by String
. That means that "81" will come before "9", which is not good. Any solution for that?
Resources that might help: