Wordpress wp_query group by custom field

前端 未结 2 1089
星月不相逢
星月不相逢 2021-01-07 06:06

I have to make a search form where i have to populate a select by the states of custom posts types. Some of them have the same state, then I would like to group-by to have a

2条回答
  •  伪装坚强ぢ
    2021-01-07 06:30

    You can try below code:

    postmeta . '.meta_key = "state"';
        }
    ?>
    
     'observatoire',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'state'
    )); 
    ?>
    
    
    

    Query Look like:

    SELECT wp_posts.*
    FROM wp_posts INNER JOIN wp_postmeta 
      ON (wp_posts.ID = wp_postmeta.post_id)
    WHERE wp_posts.post_type = 'observatoire'
      AND (wp_posts.post_status = 'publish')
      AND (wp_postmeta.meta_key = 'state' )
    GROUP BY wp_postmeta.meta_key = "state"
    ORDER BY wp_posts.post_date DESC
    

提交回复
热议问题