Title: need help with queries&#8230;
Last modified: August 21, 2016

---

# need help with queries…

 *  Resolved [nando99](https://wordpress.org/support/users/nando99/)
 * (@nando99)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/)
 * I’m using showing the last 5 or so posts from multple categories on a single 
   category page… i’m using the following code like 10 times: [http://pastebin.com/UBnVVJRy](http://pastebin.com/UBnVVJRy)
 * that page seems to load extremely show…
 * anyone have a better idea of how to write that code? I tried WP_Query but i couldnt
   get it to work right… i read about pre_get_posts but i’m not sure how that would
   work, i know the main code goes in the functions.php but how would i put it on
   the template…
 * any and all help is appreciated

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762885)
 * 1. That’s only part of a template. Is that the only query and Loop on the page?
 * 2. Why are you using tabled markup for presentation? That alone will be causing
   serious page bloat.
 *  Thread Starter [nando99](https://wordpress.org/support/users/nando99/)
 * (@nando99)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762886)
 * full page: [http://pastebin.com/MPhWzNPe](http://pastebin.com/MPhWzNPe)
 * its just the same type of query used multiple times…
 * unfortunately, i have to use tables bc we email the end result as a newsletter
   and tables are the only way to control the message output 100%
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762889)
 * > ts just the same type of query used multiple times…
 * Well, that’s probably part of the problem. You shouldn’t be using query_posts()
   multiple times. Only for customising the main Loop – and even then, use [pre_get_posts()](http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts)
   if you can (though I don’t think it will offer much in the way of a performance
   improvement).
 * However, all of the _subsequent_ queries must use WP_Query() – not query_posts().
 * How many separate queries are on that page? I stopped counting when I hit double
   figures.
 *  Thread Starter [nando99](https://wordpress.org/support/users/nando99/)
 * (@nando99)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762896)
 * 14 query_posts…
 * i tried using wp_query but i couldnt get it to only show posts that below to 
   2 specific categories… i’m trying to look for the code i used that didnt work…
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762904)
 * Well something like:
 *     ```
       <?php query_posts(array('category__and'=>array(16,4433),'showposts'=>30,'orderby'=>menu_order,'order'=>asc)); if( have_posts() ) : ?>
       [do display stuff]
       <?php endwhile; endif ?>
       ```
   
 * which is your second query on that page could be re-written as:
 *     ```
       $args = array(
       	'category__and'=>array(16,4433),
       	'posts_per_page'=>30,
       	'orderby'=>menu_order,
       	'order'=>asc
       );
       $second_query = new WP_Query( $args );
       while ( $second_query->have_posts() ) :
       	$second_query->the_post();>
       	[do display stuff]
       <?php endwhile;
       wp_reset_postdata();
       ```
   
 * Note the use of `posts_per_page` and not `showposts` which is now deprecated.
 *  Thread Starter [nando99](https://wordpress.org/support/users/nando99/)
 * (@nando99)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762926)
 * awesome!
 * quick thing, i’d only like to show the header of a section if it has posts… after
   messing with the code, i got it to show the heading with every post… smh
 *     ```
       <?php
       	$args = array(
       	'category__and'=>array(16,4433),
       	'posts_per_page'=>30,
       	'orderby'=>menu_order,
       	'order'=>asc
       	);
       	$second_query = new WP_Query( $args );
       	while ( $second_query->have_posts() ) : ?>
           <tr>
             <td class="fpb"> News & Upcoming Events</td>
           </tr>
           <?php
       	$second_query->the_post();
       	?>
           <tr>
             <td class="postbit" id="post-<?php the_ID(); ?>"><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
               <?php the_title(); ?>
               </a></strong>
               <table border="0" cellspacing="0" cellpadding="0" width="100%">
                 <tr>
                   <td><?php the_excerpt_max_charlength(200); ?></td>
                   <?php if (has_post_thumbnail()) {?>
                   <td><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 100,100 ), false, '' ); echo $src[0]; ?>" align="right" class="polaroid" style="height:50px; width:50px;" /></a></td>
                   <?php } ?>
                 </tr>
               </table></td>
           </tr>
           <?php endwhile; wp_reset_postdata(); ?>
       ```
   
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3762935)
 * Change:
 * `while ( $second_query->have_posts() )`
 * to:
 *     ```
       if( $second_query->have_posts() ) :
       while ( $second_query->have_posts() )
       ```
   
 * Then change `<?php endwhile; wp_reset_postdata(); ?>` to `<?php endwhile; endif;
   wp_reset_postdata(); ?>`. That should stop any output from being displayed if
   there are no posts to display.
 *  Thread Starter [nando99](https://wordpress.org/support/users/nando99/)
 * (@nando99)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3763070)
 * awesome! thanks!
 *  Thread Starter [nando99](https://wordpress.org/support/users/nando99/)
 * (@nando99)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3763249)
 * any other way to do this? the page still takes forever to load… about 18 seconds…
   i have w3 total cache installed as well…

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

The topic ‘need help with queries…’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [multiple categories](https://wordpress.org/support/topic-tag/multiple-categories/)
 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)
 * [queries](https://wordpress.org/support/topic-tag/queries/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [nando99](https://wordpress.org/support/users/nando99/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/need-help-with-queries/#post-3763249)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
