Basically, what you are doing right now is this:
- Get all the terms from
industry
taxonomy - For each term do a
WP_Query
to get the newest posts, iterate through them to sort the terms, creating a new array with term ids - Sort and slice the new array
- For each element in new array of terms ids do again a
WP_Query
to get the newest posts, and iterate through them
That’s a lot of WP_Query
ies. Let’s try to lower that number.
We can do it like this:
- Get all the terms from
industry
taxonomy - For each term do a
WP_Query
to get the newest posts, iterate through them to sort the terms, creating a new array - New array will be keyed with a date in
U
format, and contain both the term object and the array with post objects, so we won’t have to query them again.
Here is my suggested code:
$taxonomy = 'industry'; //taxonomy name
$terms_to_display = 5; //number of terms to display
$posts_per_term = 4; // number of posts per term to display
$terms = get_terms( array('taxonomy' => $taxonomy, 'hide_empty' => true ) );
$news_items = [];
foreach ( $terms as $term ) {
$args = array (
'post_type' => 'news',
'posts_per_page' => $posts_per_term,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array (
array (
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => array( $term->term_id ),
),
),
);
$news_query = new WP_Query( $args );
// ignore terms with not enough posts
if ( $news_query->post_count < $posts_per_term ) {
continue;
}
$news_posts = $news_query->get_posts();
// get date of newest post in this term
$newest_post_date = get_the_date( "U", $news_posts[0]->ID );
$news_items[$newest_post_date] = array(
'term' => $term,
'news' => $news_posts,
);
}
krsort( $news_items ); // sort descending by keys
$news_items = array_slice( $news_items, 0, $terms_to_display );
wp_reset_query();
I have made a few more changes as well:
1. $terms
is now array of the WP_Term
objects, not just ids
2. Made the $news_query->post_count < $posts_per_term
check slightly more readable
And how to use:
foreach ( $news_items as $item ) {
echo "<h2>" . $item['term']->name . "</h2>";
echo "<ul>";
foreach ( $item['news'] as $news_post ) {
setup_postdata( $news_post );
echo "<li>" . get_the_title() . "</li>";
}
echo "</ul>";
}
wp_reset_query();
This way, we have lowered the number of WP_Query
ies to the number of non-empty terms in the industry
taxonomy, and made the resulting array much more useful.