Title: Exclude category when using multiple loops/queries
Last modified: August 19, 2016

---

# Exclude category when using multiple loops/queries

 *  [pad](https://wordpress.org/support/users/pad/)
 * (@pad)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-when-using-multiple-loopsqueries/)
 * Hi guys,
 * I’m really stuck with this loop code, if anyone could help I’d be most grateful.
   Basically I have a ‘Featured video’ category which has a custom field of a YouTube
   URL, which then feeds into a query call in the sidebar inside an embed code, 
   to show the video. However I have 2 other loops on that page. Here is the code:
 *     ```
       <?php $my_query = new WP_Query('showposts=1');
       while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;?>
   
       *** LOOP #1: CODE FOR TOP POST ***
   
       <?php endwhile; ?>
   
       <?php if (have_posts()) : while (have_posts()) : the_post();
         if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   
       *** LOOP #2: CODE FOR REST OF POSTS ***
   
       <?php endwhile; endif; ?>
   
       <div id="sidebar"><?php $my_query2 = new WP_Query('category_name=Featured Video&showposts=1'); ?>
        <?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
   
       *** LOOP #3: CODE FROM VIDEO ARTICLE ***
   
       <?php endwhile; ?></div>
       ```
   
 * I’d like to exclude the “Featured video” category from Loop #1 and Loop #2, but
   allow it to still display the latest post from the video category afterwards.
 * I’d really appreciate a hand!

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

 *  [netbuddy34](https://wordpress.org/support/users/netbuddy34/)
 * (@netbuddy34)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-when-using-multiple-loopsqueries/#post-880688)
 * I have similiar code on the front page and would like to exclude all categories
   bar one.
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post();
         if($post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
       ```
   
 * I only want posts from “Scripts” category to come up on the homepage, and I dont
   want the hidden posts to be subtracted from the number of posts I want on the
   front page either, ie 10…still not sure how to work the code into that loop.
 *  [netbuddy34](https://wordpress.org/support/users/netbuddy34/)
 * (@netbuddy34)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-when-using-multiple-loopsqueries/#post-880697)
 * Still cant get this to work. Of course getting a reply in the support forums 
   would be a miracle but here goes.
 * This is my home.php
 *     ```
       <?php get_header(); ?>
       <div id="wrapper">
         <div id="content-wrapper">
           <div id="content">
   
       <strong>// original query runs in The (real) Loop first
       <?php while ( have_posts() ) : the_post();
           // detect posts matching our exclusion criteria
         <?php if (in_category(1)) {
               $wp_query->post_count++; // increment the post counter
               continue;
           }
           <?php endif; ?>
       <?php endwhile; ?>
       // now make a new query and show the posts for real, with the adjusted post count and filtering
       <?php $my_new_query = new WP_Query($query_string.'&showposts='.$wp_query->post_count); ?>
       // do another The Loop (and display the results this time)
       <?php while ( $my_new_query->have_posts() ) : $my_new_query->the_post();
           // detect and exclude these same posts
           <?php if (in_category(1)) { continue; }</strong>
   
             <?php
       		// check for thumbnail
       $thumb = get_post_meta($post->ID, 'Thumbnail', $single = true);
       // check for thumbnail class
       $thumb_class = get_post_meta($post->ID, 'Thumbnail Class', $single = true);
       // check for thumbnail alt text
       $thumb_alt = get_post_meta($post->ID, 'Thumbnail Alt', $single = true);
       				 ?>
             <div class="post-wrapper">
               <div class="post">
                 <div class="homepost-rating">
                   <?php if (function_exists('ratings_table')) ratings_table(); ?>
                 </div>
                 <div class="homepost-content"> <span class="titles"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                   <?php the_title(); ?>
                   </a></span>
                   <?php the_excerpt(); ?>
                 </div>
                 <div class="reviews">
                   <div class="reviewcount"><a href="<?php the_permalink(); ?>">
                     <?php comments_number('0', '1', '%' );?>
                     Reviews</a></div>
                   <div class="visitsite">
                     <?php visit_site_link('Visit Site'); ?>
                   </div>
                 </div>
               </div>
             </div>
             <?php comments_template(); ?>
             <?php endwhile; ?>
             <p class="pagination">
               <?php next_posts_link('&laquo; Previous Entries') ?>
               <?php previous_posts_link('Next Entries &raquo;') ?>
             </p>
             <?php else : ?>
             <h2 >No Results Found</h2>
             <p>Sorry, your search returned zero results. </p>
             <?php endif; ?>
           </div>
   
       <div id="mostcommentposts">
         <div id="mostcommentposts-left">
           <ul class="idTabs">
             <li><a href="#recentcomments2">Recent Comments</a></li>
             <li><a href="#topcommentors">Top Commentors</a></li>
           </ul>
   
          <div id="recentcomments2">
       <span class="toptitle"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/icon-comments2.gif" alt="icon comments" class="icons" />Recent Comments</span>
       <ul class="list2">
       <?php recent_comments(); ?>
       </ul>
       </div>
   
           <div id="topcommentors"> <span class="toptitle"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/icon-popular.gif" alt="icon reviewers" class="icons" />Top Commentors</span>
             <ul class="list2">
       <?php ns_show_top_commentators(); ?>
             </ul>
           </div>
   
         </div>
   
         <div id="mostcommentposts-right">
           <div id="adsense150">
       <?php include 'adsense150.php';?>
           </div>
         </div>
   
       </div>
   
         </div>
       <?php get_sidebar(); ?>
       <?php get_footer(); ?>
       </body></html>
       ```
   
 * The part of bolded is where I should be setting this up. All I want is for posts
   only in one category to display on the frontpage and for the counter to be updates
   so there are always 10 “Scripts” post on the frontpage.
 * Any help would be a godsend.
 *  [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-when-using-multiple-loopsqueries/#post-880698)
 * [Multiple Loops](http://codex.wordpress.org/The_Loop#Multiple_Loops)

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

The topic ‘Exclude category when using multiple loops/queries’ is closed to new 
replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [exclusion](https://wordpress.org/support/topic-tag/exclusion/)
 * [multiple loops](https://wordpress.org/support/topic-tag/multiple-loops/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [iridiax](https://wordpress.org/support/users/iridiax/)
 * Last activity: [17 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-when-using-multiple-loopsqueries/#post-880698)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
