Title: wp_query
Last modified: August 19, 2016

---

# wp_query

 *  Resolved [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp_query-1/)
 * I’ve been reading about multiple loops, and have been using query_posts up until
   now.
 * So now I read here [http://codex.wordpress.org/The_Loop](http://codex.wordpress.org/The_Loop)
   that it’s necessary to use wp_query, along the lines of
 *     ```
       <?php $first_loop = new WP_Query('showposts=200&cat=24&orderby=rand');
       while ($first_loop->have_posts()) : $first_loop->the_post(); 
   
       DO STUFF
   
       endwhile;
       ```
   
 * The problem with this is that there’s no if.. else, so that if there are no posts,
   an error message is displayed. (I want an error message if there are no posts)
 * What do I do?
 * Also, second question, it’s not made clear here [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)
   what parameters wp_query can take.
 * Are they exactly the same parameters as query_post?
 * Third question, query_post uses `$query_string .` to preserve information passed
   via a link. Is there an equivalent for wp_query?
 * Many thanks

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp_query-1/#post-1133373)
 * > The problem with this is that there’s no if
 * Couldn’t you use `<?php if ($first_loop->have_posts()) : ?>`
 * > Also, second question, it’s not made clear here [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)
   > what parameters wp_query can take.
   > Are they exactly the same parameters as query_post?
 * Yes
 * >  Third question, query_post uses $query_string . to preserve information passed
   > via a link. Is there an equivalent for wp_query?
 * Don’t believe so, but there is `$first_loop->query`
 *  [darrinb](https://wordpress.org/support/users/dbmartin/)
 * (@dbmartin)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp_query-1/#post-1133386)
 * What you’re looking for is something along the lines of this:
 *     ```
       <?php $first_query = new WP_Query('query terms in here');?>
   
       <?php if ($first_query->have_posts()) : while ($first_query->have_posts()) : $first_query->the_post(); ?>
       	DO STUFF
       	<?php endwhile; ?>
       	<?php else : ?>
       	DO STUFF IF NO POSTS
       <?php endif; ?>
       ```
   
 *  Thread Starter [richarduk](https://wordpress.org/support/users/richarduk/)
 * (@richarduk)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp_query-1/#post-1133476)
 * Thanks, MichaelH anddbmartin.
 * I wasn’t sure if if …else was allowed (no reason why it shouldn’t be, it just
   seemed rather definite in the article linked to above)
 * Much appreciated to you both
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/wp_query-1/#post-1133479)
 * If / Else is simply checking a condition, you can use it pretty much anywhere
   in PHP providing you use something valid to check..
 * You can check if something exists, if something is equal to something else, if
   it’s less than, more than….. and many many more things…
 * If / Else is used in many scripting languages…. not just PHP.. and of course 
   the syntax varies depending on the language…

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

The topic ‘wp_query’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 4 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/wp_query-1/#post-1133479)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
