0

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 ?

0

You 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',
    ),
];
2
  • 1
    Didn't directly work but you gave me the approach thank you $args = [ 'post_type'=>'game', 'posts_per_page' => 1, 'order' => 'DESC', 'orderby' =>'_game_date_key', 'meta_query' => array( array( 'key'=>'_game_home_goals_key', 'value'=>'', 'compare'=>'!=' ), array( 'key'=>'_game_away_goals_key', 'value'=>'', 'compare'=>'!=' ) ) ]; that's the one I used May 8 '20 at 10:20
  • What if I have a date string inside how do I sort it as a date, not a normal string? May 14 '20 at 11:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.