I’m trying to create a simple loop, that gets the latest post of 3 selected categories. I’ve searched for something similar to learn from, but most are overly complex.
At the moment, I have:
<?php
// WP_Query arguments
$args = array (
'category_name' => array('lifestyle', 'fashion', 'beauty')
);
// The Query
$query = new WP_Query( $args[0] );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
I assume I’d need a foreach
loop, but I’m unsure how to implement it in this scenario, and for just the latest of each post?
Any help would be great.
It’s not possible to get one posts per category with one simple query, and even a complex query will take more time than 3 separate query. So, if you want simplest, then this is the solution –