Title: Too Many Queries
Last modified: August 18, 2016

---

# Too Many Queries

 *  [andymike](https://wordpress.org/support/users/andymike/)
 * (@andymike)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/too-many-queries/)
 * I am currently using this code to query specific posts by month and year in
    
   a page template, so I have to repeat this query 12 times for every year everytime
   the page is loaded:
 * `<?php query_posts('cat=2&year=2006&monthnum=1');
    if ($wp_query->post_count 
   >= 1) { ?> <---do stuff--> <?php } ?>
 * I am worried that this is too many queries. Is there a better way of doing
    this?
   Perhaps something along these lines:
 * `<?php query_posts('cat=2&year=2006');
    if ($wp_query->month_num = 1) && ($wp_query-
   >post_count >= 1) { ?> <---do stuff--> <?php } ?>
 * I know month_num won’t work but is there any way of getting the month number 
   so I can pass it in the PHP if statement? That way I only have to run one query
   for each year.
 * Thanks

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

 *  [muelheim](https://wordpress.org/support/users/muelheim/)
 * (@muelheim)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/too-many-queries/#post-487481)
 * You have to build a new sql-query like
 * SELECT COUNT(*), year, month FROM texte GROUP BY year, month
 * which returns an array with count, year and month in just one query
 *  Thread Starter [andymike](https://wordpress.org/support/users/andymike/)
 * (@andymike)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/too-many-queries/#post-487600)
 * Thanks muelheim,
 * Could you explain a little bit further how to build the sql-query?
 * I appreciate your help.

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

The topic ‘Too Many Queries’ is closed to new replies.

## Tags

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

 * 2 replies
 * 2 participants
 * Last reply from: [andymike](https://wordpress.org/support/users/andymike/)
 * Last activity: [19 years, 5 months ago](https://wordpress.org/support/topic/too-many-queries/#post-487600)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
