Hope this can help:
function wpse_display_post_counts_by_categories() {
$categories = get_categories();
$container = [];
foreach ($categories as $category) {
$args = [
'post_type' => 'post', // Change to your post type.
'post_status' => 'publish',
'category' => $category->term_id,
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1
];
$posts = get_posts($args);
if ( count($posts) ) { // Indexing by post date of the latest post in the category.
$container[strtotime($posts[0]->post_date)] = [
'id' => $category->term_id,
'name' => $category->name,
'counts' => count($posts)
];
}
}
krsort($container); // Sort by key(post date), from high to low to get latest updated categories.
$values = array_slice($container, 0, 3, true); // Get only 3 latest updated categories.
foreach ($values as $key => $value) {
$output = sprintf( __('%s, there are %d articles posted in category <a href="https://wordpress.stackexchange.com/questions/223927/%s">%s</a>', 'text-domain'), date_i18n('Y/m/d', $key), $value['counts'], esc_url( get_term_link($value['id'], 'category') ), $value['name']);
echo '<p>' . $output . '</p>'; // Whatever markup you need.
}
}
IMHO: Currently, I don’t figure out a more efficient way to do this. I don’t know how you handle the $categories
when there are too many to loop through. I don’t recommend to do this. It’s too much just for printing out some statuses.