Title: Simpler code than this?
Last modified: August 19, 2016

---

# Simpler code than this?

 *  Resolved [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/)
 * This is a second loop, which is designed not to show any posts that were in the
   first loop.
 * But – is there a way of simplifying the latter bit, given that I have a hundred
   thumbnails perhaps to show?
 *     ```
       <?php query_posts('order=DESC&showposts=100&cat=3'); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post();
       if($post->ID == $do_not_duplicate[0] ||
       $post->ID == $do_not_duplicate[1] ||
       $post->ID == $do_not_duplicate[2] ||
       $post->ID == $do_not_duplicate[3] ||
       $post->ID == $do_not_duplicate[4] ||
       $post->ID == $do_not_duplicate[5] ||
       $post->ID == $do_not_duplicate[6] ||
       $post->ID == $do_not_duplicate[7] ||
       $post->ID == $do_not_duplicate[8] ||
       $post->ID == $do_not_duplicate[9] ||
       $post->ID == $do_not_duplicate[10] ||
       $post->ID == $do_not_duplicate[11] ||
       $post->ID == $do_not_duplicate[12] ||
       $post->ID == $do_not_duplicate[13] ||
       $post->ID == $do_not_duplicate[14] ||
       $post->ID == $do_not_duplicate[15] ||
       $post->ID == $do_not_duplicate[16]
       ) continue; update_post_caches($posts); ?>
       ```
   

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

 *  [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950784)
 * You should have your “do not duplicate” stuff set up in your *first* loop. The
   second loop should check against the first using the variable.
 * <?php //Loop 1
    $initial = new WP_query(‘cat=1’); if($initial->have_posts()) :
   while($initial->have_posts()) : $initial->the_post(); $do_not_duplicate = $post_ID;?
   > stuff for first section here <?php endwhile; endif; ?>
 * <?php Loop 2
    $second = new WP_query(‘order=DESC&showposts=100&cat=3’); if($second-
   >have_posts()) : while($second->have_posts()) : $second->the_post(); if( $post-
   >ID == $do_not_duplicate ) continue; update_post_caches($posts); ?> stuff for
   second display here <?php endwhile; endif; ?>
 *  Thread Starter [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950786)
 * I think that’s what I have. The first loop uses an array to store all the post
   IDs – the second loop makes sure that specified IDs in that array are not output.
   It does this through `
    $post->ID == $do_not_duplicate[14] || $post->ID == $do_not_duplicate[
   15] || `
 * etc.
 * I just wondered if there was a simpler way of saying, do not output these posts.
   A little snippet of code to replace what promises to be massive long list.
 *     ```
       <?php $my_query = new WP_Query('cat=3&showposts=12');
       if (have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
         $do_not_duplicate[] = $post->ID;?>
           <!-- Do stuff... -->
        <?php endwhile; ?>
       <?php else : ?>
       <h2>You forgot to create any posts !</h2>
       <?php endif; ?>    
   
       SECOND LOOP
   
       <?php query_posts('order=DESC&showposts=100&cat=3'); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post();
       if($post->ID == $do_not_duplicate[0] ||
       $post->ID == $do_not_duplicate[1] ||
       $post->ID == $do_not_duplicate[2] ||
       $post->ID == $do_not_duplicate[3] ||
       $post->ID == $do_not_duplicate[4] ||
       $post->ID == $do_not_duplicate[5] ||
       $post->ID == $do_not_duplicate[6] ||
       $post->ID == $do_not_duplicate[7] ||
       $post->ID == $do_not_duplicate[8] ||
       $post->ID == $do_not_duplicate[9] ||
       $post->ID == $do_not_duplicate[10] ||
       $post->ID == $do_not_duplicate[11] ||
       $post->ID == $do_not_duplicate[12] ||
       $post->ID == $do_not_duplicate[13] ||
       $post->ID == $do_not_duplicate[14] ||
       $post->ID == $do_not_duplicate[15] ||
       $post->ID == $do_not_duplicate[16]
       ) continue; update_post_caches($posts); ?> 
   
        <!-- Do stuff... -->
       <?php endwhile; ?>
       <?php else : ?>
       <h2>You forgot to create any posts !</h2>
       <?php endif; ?>
       ```
   
 *  Moderator [Ipstenu (Mika Epstein)](https://wordpress.org/support/users/ipstenu/)
 * (@ipstenu)
 * 🏳️‍🌈 Advisor and Activist
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950788)
 * How are you trying to separate the posts? I mean, I’m assuming you have columns
   or something. If the number of posts you want to ignore is a static number (like‘
   ignore the first 16 posts in cat3’) you can do this:
 * `<?php query_posts('order=DESC&showposts=100&cat=3&offset=16'); ?>`
 *  Thread Starter [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950808)
 * Ipstenu – it was worth a try but it didn’t work.
 * All I want to simplify is this bit. Just for my own satisfaction, rather than
   having a hundred lines of this.
 *     ```
       if($post->ID == $do_not_duplicate[0] ||
       $post->ID == $do_not_duplicate[1] ||
       $post->ID == $do_not_duplicate[2] ||
       $post->ID == $do_not_duplicate[3] ||
       $post->ID == $do_not_duplicate[4] ||
       $post->ID == $do_not_duplicate[5] ||
       $post->ID == $do_not_duplicate[6] ||
       $post->ID == $do_not_duplicate[7] ||
       $post->ID == $do_not_duplicate[8] ||
       $post->ID == $do_not_duplicate[9] ||
       $post->ID == $do_not_duplicate[10] ||
       $post->ID == $do_not_duplicate[11] ||
       $post->ID == $do_not_duplicate[12] ||
       $post->ID == $do_not_duplicate[13] ||
       $post->ID == $do_not_duplicate[14] ||
       $post->ID == $do_not_duplicate[15] ||
       $post->ID == $do_not_duplicate[16]
       ) continue;
       ```
   
 *  [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950817)
 * Maybe it would be clearer if we knew what it was you were trying to do? From 
   your code, it looks like you’re trying to not display any posts that have an 
   ID of “16” or less, but you want to display “17”. If you’re only trying to show
   the most recent post, you’re *really* going the wrong way with this. What are
   you trying to accomplish?
 *  Thread Starter [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950821)
 * Yeah, sorry.
 * It got a bit scrappy because it’s in development.
 * The first twelve posts should be displayed in one div.
 * Another div a little way down the page shows the next 88 or however many posts.
 * Each post is going to be a thumbnail of a t shirt. That comes next, with custom
   fields.
 * [http://www.tshirtcool.com/brilliant/funny-tshirts/](http://www.tshirtcool.com/brilliant/funny-tshirts/)
 * You can see the first twelve posts have only the permalink showing.
 * The next few posts have only the content showing. This is so that I can see if
   the multiple loops are working or not.
 * They are working. No problems with that. Just that I want that last bit of code
   in loop 2 tidied up a bit.
 *  [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950991)
 * Okay, I see. Well will the “first 12” ever change? i imagine they would. IMO,
   what I would do is create a simple category for the ones you want to display 
   in the first section. Anything you want added to that category, just check the
   box. Use a query on your template to pull in stuff only for that category. (If
   this is what you’ve already done – I’m thinking out loud here.) I’m getting the
   idea though that that list of post ID’s will be contstantly changing – I can’t
   imagine it’s just the first 12 and they’ll be like that forever. So I’d say choose
   the most recent 12 out of that category, and do what you’re doing:
 *     ```
       <?php //Loop 1
       $initial = new WP_query('cat=1&showposts=12');
       if($initial->have_posts()) : while($initial->have_posts()) : $initial->the_post();
       $do_not_duplicate = $post_ID;?>
       stuff for first section here
       <?php endwhile; endif; ?>
   
       <?php //Loop 2
       $second = new WP_query('order=DESC&showposts=100');
       if($second->have_posts()) : while($second->have_posts()) : $second->the_post();
       if( $post->ID == $do_not_duplicate ) continue;
       update_post_caches($posts); ?>
       stuff for second display here
       <?php endwhile; endif; ?>
       ```
   
 * Where the first loop will display the most recent 12 items in category 1, and
   the last loop will just display the most recent 100 posts made *excluding* he
   ones from the first loop.
 * Does that make more sense?
 * This is, of course if you want those first 12 posts to be variable. If they’re
   static, and those 12 shirts will always be the same (and since you’re using custom
   fields) then perhaps this might be of more use to you:
 * [Get Post Custom](http://codex.wordpress.org/Function_Reference/get_post_custom)
 * Then what you could do is display those 12 shirts via custom fields, and then
   just do ALL posts at the bottom. Since the content of the divs will no longer
   be the same, you don’t have to worry about it looking like duplicated content.
   Does that make sense?
 *  Thread Starter [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-950996)
 * Thanks for your time, doodlebee.
 * The first 12 images will change – I shall probably make the whole pageful of 
   images random, so that different ones appear at the top where people are likely
   to look first. That’s to be decided later, anyway.
 * I can see what you’re doing, and I might head that way. But at the end of the
   day I want everything simple, simple, simple, so that I don’t get buried under
   mountains of administration. To that end all the shirts on this page will probably
   be in the one category. But I’ve yet to find out what exactly is possible, and
   what works best – for example, having the top section just for most popular designs
   etc.
 * What I would really like is one line of code to replace the bottom 100 lines 
   of code (if I have a hundred images).
 * There must be a way of accessing an array that’s a bit more elegant than that
   marathon list and simply says, if $post–>ID is identical to array $do_not_duplicate
   with a key from 1 to 100 then don’t display.
 * 🙂
 *  Thread Starter [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-951072)
 * OK, here’s the simplified code for multiple loops, with the second loop not showing
   any of the posts that appeared in the first loop.
 *  The Codex is definitely muddled in the section dealing with multiple posts not
   showing up in multiple loops.
 * The simplified code at the end came from Sitepoint, so thanks to them!
 * > `
   > MULTIPLE LOOPS, SECOND LOOP NOT SHOWING ALL POSTS
   >  THAT APPEARED IN THE FIRST
   > LOOP
   > ******************* LOOP 1 **********************
   > <?php $my_query = new WP_Query(‘cat=3&showposts=12’);
   >  if (have_posts()) :while(
   > $my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate[] = $post-
   > >ID;?>
   > (DO STUFF)
   > <?php endwhile; ?>
   >  <?php else : ?> <h2>You forgot to create any posts !</h2
   > > <?php endif; ?>
   > ******************* LOOP 2 **************************
   > <?php query_posts(‘order=DESC&showposts=100&cat=3’); ?>
   >  <?php if (have_posts()):
   > while (have_posts()) : the_post(); if(in_array($post->ID, $do_not_duplicate))
   > continue; update_post_caches($posts); ?>
   > (DO STUFF)
   > <?php endwhile; ?>
   >  <?php else : ?> <h2 class=”not-found”>Oops! You forgot 
   > to create any posts !</h2>
   > <?php endif; ?>
 * `
 *  [westondeboer](https://wordpress.org/support/users/poil11/)
 * (@poil11)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-951190)
 * This is great invaluable code!

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

The topic ‘Simpler code than this?’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 4 participants
 * Last reply from: [westondeboer](https://wordpress.org/support/users/poil11/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/simpler-code-than-this/#post-951190)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
