Title: Multiple Loops &#8211; First loop constrained by second loop
Last modified: August 19, 2016

---

# Multiple Loops – First loop constrained by second loop

 *  Resolved [valuxes](https://wordpress.org/support/users/valuxes/)
 * (@valuxes)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/)
 * I have two loops: one in the Header and the standard main one.
 * The one in the Header is supposed to retrieve records that match 2 categories
   for the whole current year. What is happening is that the first loop is only 
   returning records in the matched categories from the 8 records returned by the
   main loop (main loop set to return only last 8 records).
 * Header loop:
 * [http://wordpress.pastebin.ca/1834779](http://wordpress.pastebin.ca/1834779)
 * The main loop is unchanged.
 * I thought that $my_query = new WP_Query would retrieve all records that match
   cat_1 and cat_2 for the whole year. Instead it returns only for those in the 
   8 records returned by the main loop. So if the main loop does not contain any
   records with those 2 categories, the Header loop displays nothing (even if there
   are lots for this year).
 * Thanks for any help.

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422048)
 * I am familiar with ‘category__in’, but not ‘category_name__in’. Are you sure 
   that is a valid parameter? Try using ‘category__in’ with category IDs in the 
   array.
 *  Thread Starter [valuxes](https://wordpress.org/support/users/valuxes/)
 * (@valuxes)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422077)
 * Thanks vtxyzzy for the suggestion, it worked!
 * The category_name__in used to work in v2.9.1 — it stopped working when I upgraded
   to v2.9.2.
 * category__in with the IDs works again. But it does not return ALL the posts that
   match the categories (name or ID) for the year.
 * Is the Year test coded correctly? I would like to display all the recs for the
   current Year or current Year and Month.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422082)
 * The year looks OK. Maybe you are limited by the number of posts per page. Try
   setting the posts_per_page in the args like this:
 *     ```
       <?php $my_query = new WP_Query(array(
         'category_name__in'=>array(cat_1,cat_2),
         'year'=>date('Y'),
         'order_by'=>date,
         'order'=>'DESC',
         'posts_per_page' => -1)); ?>
       ```
   
 *  Thread Starter [valuxes](https://wordpress.org/support/users/valuxes/)
 * (@valuxes)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422100)
 * Perfect!
 * I also added a:
 * ‘monthnum’=>date(‘m’) to restrict for the month.
 * Thanks so much vtxyzzy!!!
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422107)
 * Hmm–don’t see any reference to `category_name__in` in core code. Did a plugin
   provide that?
 *  Thread Starter [valuxes](https://wordpress.org/support/users/valuxes/)
 * (@valuxes)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422111)
 * MichaelH,
 * Not sure. I probably saw category_name used on one of the example pages and added
   the __in myself (duh!). It “seemed” to work until now. 😉

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

The topic ‘Multiple Loops – First loop constrained by second loop’ 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/)
 * 6 replies
 * 3 participants
 * Last reply from: [valuxes](https://wordpress.org/support/users/valuxes/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-first-loop-constrained-by-second-loop/#post-1422111)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
