WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom loop to show posts that meet a certain set of $args like this:

<?php $recent = new WP_Query( $orderargs );     
        while($recent->have_posts()) : $recent->the_post();?>

This works great when the $orderargs bring up posts, but in some situations, there will be no posts, and I want to show some default content then.

So what do I add to this / how do I restructure this so that if the query has no posts, display default content instead.

Thank you, Ian

share|improve this question
up vote 0 down vote accepted

You do that the same way like with regular posts:

if ( $recent->have_posts() )
        // print posts
    print 'no posts found';
share|improve this answer
Thanks toscho, That worked! – IanB Oct 27 '12 at 14:04

Just you use the regular loop.

<?php $recent = new WP_Query( $orderargs ); while($recent->have_posts()) : $recent->the_post();?>

type your code here.

<?php endwhile; else: ?>

Put your default content here.

<?php endif; ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.