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

Scenario: Page displays a list of titles for a custom post type (video) with a selected "category" (in the taxonomy its called the subject). For example videos with the subject of "creative"

Problem If there are titles present that match the the criteria then the list is displayed correctly but if there are no titles then the message "No Titles Found" fails to appear on the page. In other words the echo statement is failing.

My Code

<?php $subject = $_GET['subject'];

if ($subject=="") $subject = 'creativity';?><?php /*If there is no value returned then the loop defaults to searching for items beginning with 'Creativity' since its the first term.*/ ?>
<h2>Video Titles For <span class="colorBlack"><?php echo $subject ?></span></h2> <?php     /*This needs to be outside of the loop*/?>

$loop = new WP_Query( array( 'post_type' => 'video', 'subject' => $subject,     'posts_per_page' => 10 ) ); ?>
<?php if (have_posts() ):?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
                            <div class="glossarybody">
                                <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
            <?php endwhile;
      else: echo 'No Titles Found';

Looking at the codex the form of the 'if else is given as:

if ( have_posts() ) :
while ( have_posts() ) : the_post();
    // Your loop code
else :
echo wpautop( 'Sorry, no posts were found' );

So I seem to have the correct syntax and since the 'if' section functions correctly I assume it has correct syntax.

I'd appreciate any pointers as to where I'm going wrong.


share|improve this question
up vote 3 down vote accepted

Try $loop->have_posts() on if statement. See if it fixes the issue.

share|improve this answer
Thanks, that solved it. – dorich Aug 19 '11 at 14:02
@dorich Mark it as answered. – toscho Aug 19 '11 at 17:50
@toscho: Thanks. However, I need your guidance. Is there a particular method of marking a question as answered? I looked around for a button, similar to the jquery forum, that marks a question as answered but didn't see one. Previously in Stackoverflow I was chastized for adding "Answered" to a subject. What is the appropriate protocol on this board? – dorich Aug 19 '11 at 20:57
@dorich: there is a "Tick" mark under the voting option on every answer. Just click this and the answer will select as right answer ;) – Sisir Aug 19 '11 at 20:59
@Sisir: Done, thanks. – dorich Aug 20 '11 at 6:41

Why use WP_Query? I think you can just use get_posts.
I've not tested using args that is not in the list, but it should work.

This is what I would do.

  'subject' => $subject,
  'post_type' => 'video',
  'post_status' => 'publish',
  'posts_per_page' => 10

$videos = get_posts( $args );

$has_video = true;

// Loop through the posts
foreach ($videos as $video) :  setup_postdata($video); ?> 

  // We assume  that all posts have a title.
  if(!empty(get_the_title()) {
    <div class="glossarybody">
      <?php the_title(); ?>   
      <?php the_excerpt(); ?>
  } else {
    $has_video = false;   

  _e('Sorry, no posts were found');
share|improve this answer
Thanks for your detailed response. I'll try this out but in the meantime wanted to acknowledge your help. – dorich Aug 19 '11 at 14:04

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.