guys, I have a custom post type called "game" and it has some custom fields (get_post_meta) which I access by their keys "_game_date_key", "_game_home_goals_key" and "_game_away_goals_key".
How do I use the WP_Query() to return posts sorted by the "_game_date_key" and ("_game_home_goals_key" and "_game_away_goals_key") are numbers ?
guys, I have a custom post type called "game" and it has some custom fields (get_post_meta) which I access by their keys "_game_date_key", "_game_home_goals_key" and "_game_away_goals_key".
How do I use the WP_Query() to return posts sorted by the "_game_date_key" and ("_game_home_goals_key" and "_game_away_goals_key") are numbers ?
Share Improve this question asked May 8, 2020 at 9:14 Isakiye AfashaIsakiye Afasha 1371 silver badge8 bronze badges1 Answer
Reset to default 0You should add arguments to WP_Query to sort by your keys in the order you want. The compare value can be your filter (larger, smaller, etc)
$args = [
'meta_query' => array(
'relation' => 'AND',
'event_start_date_clause' => array(
'key' => '_game_date_key',
'compare' => 'EXISTS',
),
'event_start_time_clause' => array(
'key' => '_game_home_goals_key',
'compare' => 'EXISTS',
),
'event_start_time_clause' => array(
'key' => '_game_away_goals_key',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'_game_date_key' => 'ASC',
'_game_home_goals_key' => 'ASC',
'_game_away_goals_key' => 'ASC',
),
];