最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I query and sort custom-post type using WP_Query

programmeradmin3浏览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 ?

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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',
    ),
];
发布评论

评论列表(0)

  1. 暂无评论