asr asr - 6 months ago 34
PHP Question

show a post from selected categories wordpress

I'm trying to show one post from several categories. My code just shows the first category post :\ any advice?


$args = array(
'cat' => 1,15,
'post_type' => 'post',
'posts_per_page' => '1',

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ($query->the_post()):

the_post_thumbnail(array(200, 200));

<?php endwhile;


You defined 'posts_per_page' => '1' so you are getting exactly what you ask: 1 post. Either from category 1 or 15, whichever is the most recent post. If you want 1 post from EACH category, I would just loop your code, with a different category each time (just 1).

Only thing is, that will be in order of the category IDs you give and not sorted on date on something else. Also, if you have a post in multiple categories, you might end up with the same post twice.