Title: Hide code if no posts are found?
Last modified: August 21, 2016

---

# Hide code if no posts are found?

 *  Resolved [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/hide-code-if-no-posts-are-found/)
 * So I have this chunk of code in my right sidebar. I want to hise the entire module
   if there are no posts found instead of displaying “no posts found”. How would
   I go about doing this? Please incorporate your code into mine below if possible.
   i really appreciate any assistance you awesome wordpressers can provide!
 *     ```
       <div id="popularApps" class="module">
           <h3>Popular Sleep Apps</h3>
           <div class="moduleContent">
             <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 3 ); ?>
             <?php $queryObject = new WP_Query($query); ?>
             <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
             <div class="entry"><a href="/affiliates/<?php echo basename(get_permalink()); ?>/"><?php echo get_the_post_thumbnail( get_the_ID(), array(43,43)); ?>
               <p><?php if (strlen($post->post_title) > 24) { echo substr(the_title($before = '', $after = '', FALSE), 0, 24) . '...'; } else { the_title();} ?></p></a>
               <div class="clear"></div>
             </div>
             <?php endwhile; else: ?>
             <p>No Apps are available at this time</p>
             <?php endif; ?>
             <?php wp_reset_query(); ?>
           </div>
           <div class="moduleFooter"><a href="/affiliates/apps/">View More</a>
             <div class="clear"></div>
           </div>
         </div>
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/hide-code-if-no-posts-are-found/#post-3735877)
 * Try it with this:
 *     ```
       <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 3 ); ?>
        <?php $queryObject = new WP_Query($query); ?>
        <?php if( $queryObject->have_posts() ) : ?>
       <div id="popularApps" class="module">
           <h3>Popular Sleep Apps</h3>
           <div class="moduleContent">
             <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
             <div class="entry"><a href="/affiliates/<?php echo basename(get_permalink()); ?>/"><?php echo get_the_post_thumbnail( get_the_ID(), array(43,43)); ?>
               <p><?php if (strlen($post->post_title) > 24) { echo substr(the_title($before = '', $after = '', FALSE), 0, 24) . '...'; } else { the_title();} ?></p></a>
               <div class="clear"></div>
             </div>
             <?php endwhile; else: ?>
             <p>No Apps are available at this time</p>
             <?php endif; ?>
           </div>
           <div class="moduleFooter"><a href="/affiliates/apps/">View More</a>
             <div class="clear"></div>
           </div>
         </div>
        <?php endif; ?>
        <?php wp_reset_query(); ?>
       ```
   
 *  Thread Starter [danalydesign](https://wordpress.org/support/users/danalydesign/)
 * (@danalydesign)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/hide-code-if-no-posts-are-found/#post-3735879)
 * keesiemeijer, no matter what anyone saysd about you – I know that you ARE the
   man! Thanks for keeping me sane!
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/hide-code-if-no-posts-are-found/#post-3735885)
 * You’re welcome. I’m glad you’ve got it resolved 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Hide code if no posts are found?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/hide-code-if-no-posts-are-found/#post-3735885)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
