There seems to be something wrong with the logic of what I'm doing.
I just want to display the latest post in a custom term. But instead I'm displaying every post for that term, even though I am using posts_per_page => 1
I have a taxonomy called "twin" and the term "double portrait", I have 2 posts in a category called "celebrity", both with the term "double portrait". The code I have outputs in the right category fine, but as I say it's outputting for each post instead of just the one.
Here's the code I'm using:
$post_type = 'post';
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug
)
),
'posts_per_page' => 1,
'ignore_sticky_posts' => true,
'orderby' => 'modified',
);
$posts = new WP_Query( $args );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
if(in_category($str)) {
if(has_term('double portrait','twin')) {
echo the_excerpt();
}
}
endwhile; endif;
endforeach;
endforeach;
posts $terms = get_terms( $taxonomy );
? That must be a typo?