List taxonomy terms plus their latest post ordered by post date
I have the following code:
$custom_terms = get_terms('columna');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' = 'post',
'posts_per_page'=1,
'orderby' = 'date',
'order' = 'DESC',
'suppress_filters' = true,
'tax_query' = array(
array(
'taxonomy' = 'columna',
'field' = 'slug',
'orderby' = 'date',
'order' = 'DESC',
'suppress_filters' = true,
'terms' = $custom_term-slug,
),
),
);
$loop = new WP_Query($args);
if($loop-have_posts()) {
echo 'h4'.$custom_term-name.'/h4';
while($loop-have_posts()) : $loop-the_post();
echo 'a href="'.get_permalink().'"'.get_the_title().'/abr'.get_the_date().'br';
endwhile;
}
}
It works, but it gets me something like this:
* term 1
* post 1 - 2019-12-01
* term 2
* post 2 - 2020-01-01
What I'd need is that the term with the most recent post, goes first in the list:
* term 2
* post 2 - 2020-01-01
* term 1
* post 1 - 2019-12-01