分类排序数组


Sorting Array of Categories

我正试图对以下数组进行排序,以便在foreach循环中使用,该循环输出鹅溪、瞌睡溪和fobr中每一个类别的最新帖子。

我想按发布日期对这个数组进行排序,但我对如何实现这一点有些困惑。只向WP_Query参数添加多个类别并删除foreach循环会更好吗?

$feed_sources = array('goose-creek','sleepy-creek','fobr');

foreach ($feed_sources as $feed) {
$args = array('category_name' => $feed, 'posts_per_page' => 1);
$show = new WP_Query($args);
$show->the_post();

以下代码使用关联数组,并将类别作为键输出,将日期作为整数输出到值中。

Arsort()然后将数组从高排序为低

$feed_sources = array('goose-creek','sleepy-creek','fobr');
$dates_array = array();
foreach ($feed_sources as $feed) {
  $args = array('category_name' => $feed, 'posts_per_page' => 1);
  $show = new WP_Query($args);
  $show->the_post();
  $dates_array[$feed] = the_date('YmdHis','','',false);
}
arsort($dates_array);
$ordered_sources = array_keys($dates_array);
foreach ($ordered_sources as $feed) {
  $args = array('category_name' => $feed, 'posts_per_page' => 1);
  $show = new WP_Query($args);
  $show->the_post();