I want to change a WP_query parameter if there is not any posts matching the query. Specifically I want to change the monthnum value, if there is not any posts to show of the current month, I want to run the query again with the new monthnum value.
$query = new WP_Query (array('category_name'=> $cat,'year' => $ano, 'monthnum' => $monthnum, 'posts_per_page' => $posts_per_page, 'post__not_in' => array($id)));
if ($query->have_posts()) : ?>
while ($query->have_posts()) : $query->the_post();
//do something
endwhile;
else:
$monthnum = $monthnum - 1;
if($monthnum == 0){
$monthnum = 12;
}
/*
here I want to run the query again with the new $monthnum value
*/
endif;
wp_reset_query();
Do you know the solution? Help me, please! Thanks.