Title: Multiple Loops &#8211; Embedded Loop
Last modified: August 19, 2016

---

# Multiple Loops – Embedded Loop

 *  [valuxes](https://wordpress.org/support/users/valuxes/)
 * (@valuxes)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/multiple-loop-embedded-loop/)
 * I have read [http://codex.wordpress.org/The_Loop#Multiple_Loops](http://codex.wordpress.org/The_Loop#Multiple_Loops)
   and all the posts in the forum dealing with multiple loops — and still can’t 
   get my embedded loop to work properly.
 * Loop1 (Outerloop) is the WP basic loop and retrieves only 8 posts at a time on
   a page, with pagination to navigate to the prev/next page.
 * Inside this loop, I embed a second loop to retrieve all titles for a certain 
   category. To do this, I used a new query:
 *     ```
       <!-- Loop1 -->
       <?php if (have_posts()) : ?>
         <?php while (have_posts()) : the_post(); ?>
   
           <?php $my_query = new WP_Query(array(
           'category_name'=>press_releases,
           'year'=>date('Y'),
           'order_by'=>date,
           'order'=>'DESC',)); ?>
   
           <!-- Loop2 -->
           <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <?php endwhile; ?>
   
         <?php endwhile; ?>
       <?php endif; ?>
       ```
   
 * Instead of getting all the titles for that category _for the whole year_, all
   I get are the titles for that category _for the posts retrieved in Loop1_.
 * Of course, I do not want to mess up pagination for Loop1.
 * Thanks in advance for any help.

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/multiple-loop-embedded-loop/#post-1024922)
 * Try this for your “second loop to retrieve all titles for a certain category”
 *     ```
       <?php
       // posts from this year, in category id 1, exclude the current posts, display 5 posts, ignore sticky posts
           $args=array(
             'year'=>date('Y'),
             'cat' => 1,
             'post__not_in' => array($post->ID),
             'numberposts'=>5,
             'caller_get_posts'=>1
           );
       $catposts=get_posts($args);
       if ($catposts) {
       foreach($catposts as $catpost) {
       echo "<p><a href='".get_permalink($catpost->ID)."'>".$catpost->post_title."</a></p>";
       }
       }
       ?>
       ```
   
 *  Thread Starter [valuxes](https://wordpress.org/support/users/valuxes/)
 * (@valuxes)
 * [16 years, 12 months ago](https://wordpress.org/support/topic/multiple-loop-embedded-loop/#post-1025203)
 * Thanks, will try

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

The topic ‘Multiple Loops – Embedded Loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [valuxes](https://wordpress.org/support/users/valuxes/)
 * Last activity: [16 years, 12 months ago](https://wordpress.org/support/topic/multiple-loop-embedded-loop/#post-1025203)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
