查询每个类别的最新帖子

I know there are quite a few questions about wp_queries but none are quite answering what I want.

I was wrecking my brain on how to query 1 most recent post from each of my blog categories but using the [] notation instead of an array:

function get_latest_posts($query_args, $grid_name) {
  $categories = array(1593, 1594, 1595, 1596, 1597);

  if ($grid_name == 'get_latest_posts') {
    $query_args['posts_per_page'] = 1;
  } 
  return $query_args;
}
add_filter('tg_wp_query_args', 'get_latest_posts', 10, 2);
// This returns one post but just one post, not one post per category

Using this style of query_args instead of a "traditional" array, would it be possible to have something like:

foreach($categories as $category) {
  return $query_args;
}
// Something like this would be awesome ❥

Thank you in advance! Sorry I'm such a newbie.

Try this:

<?php
function post_from_each_category(){
  $categories = array(1593, 1594, 1595, 1596, 1597);
  $posts = [];
  foreach($categories as $category) {
    $posts[] = get_posts([
      'post_type' => 'post',
      'posts_per_page' => 1,
      'tax_query' => [
        [
          'taxonomy' => 'category',
          'field'    => 'term_id',
          'terms'    => [$category],
        ],
      ],
    ]);
  }
  return $posts;
}

I think it will solve your problem.

</div>