It would be great to make a single DB request but I don’t know how to do that either. I have this code and it works but I want to change it to be more efficient:
$cs = get_categories(array(
'orderby' => 'name',
'order' => "ASC",
));
$params = array(
'last_five_articles' => array()
);
foreach ($cs as $c) {
$rp = wp_get_recent_posts(array(
'numberposts' => 5,
'post_status' => 'publish',
'category' => $c->term_id
));
foreach ($rp as $idx => $crt)
{
$rp[$idx]["post_content"] = NULL;
$rp[$idx]["url"] = esc_url(get_permalink($crt["ID"]));
$rp[$idx]["is_current"] = $crt["ID"] === get_the_ID();
}
$params['last_five_articles'][$c->slug] = $rp;
}
Update 1
The fields that I use from each post are:
url
(custom field)is_current
(custom field)post_title
post_date
The rest should be ignored.