reducing the amount of wp_query calls

I’m working on a site where we’re introducing drop down panels from the menu which will display thumbnails from various tags on the site.

Each panel has a left column displaying a featured post, and a right column displaying 6 thumbnails; on each tab then I’m doing two wp_query loops, the first to display the featured post image, and the second to display the 6 thumbnails (with featured post excluded).

Read More

I’m wondering though if this is the most efficient way of doing things, as there are 10 tabs, so that’s 20 uses of wp_query in the main menu.

Example code for one of the tabs below:

    <div class="displaymenu"><div class="menucontent">

        <div class="leftdisplaymenu padding">

        <?php 
        global $quote_id;
        $quote_id=array(); // setup an array to store post ids to exclude from future queries
        $argsformainquotes=array(
        'posts_per_page' => '1',
        'post_status' => 'publish',
        'tag' => 'quotes',


        );


        $mainquotes = new WP_Query($argsformainquotes);
        while ($mainquotes->have_posts()) : $mainquotes ->the_post();
$idforquote=get_the_id(); $quote_id[]=$idforquote; // add the id of this post to exclusion array
        $post_thumbnail_id = get_post_thumbnail_id();
    $post_thumbnail_url = wp_get_attachment_image_src( $post_thumbnail_id,'bigfeature'); ?><div class="mainimg"><a href="<?php the_permalink();?>" target="_blank"><img src="<?php echo $post_thumbnail_url[0];?>" alt=" <?php the_title();?>"  class="thumblist"  height="240" width="240" /></a><div class="imgtitle"><a href="<?php the_permalink();?>" title="<?php the_title();?>" class="tablinktitles"><?php the_title();?></a></div><br /><?php $small_excerpt=get_the_excerpt();echo substr($small_excerpt,0,100);?><br /><br /><a href="<?php the_permalink();?>" class="tablinktitles">read more...</a></div><?php endwhile;?>



        <div></div></div>

        <div class="rightdisplaymenu"><ul><?php $argsforquotes=array(
        'posts_per_page' => '6',
        'post_status' => 'publish',
        'tag' => 'quotes',
        'post__not_in' => $quote_id,



        );


        $quotes = new WP_Query($argsforquotes);
        while ($quotes->have_posts()) : $quotes ->the_post();?>
        <li><?php 



        $post_thumbnail_id = get_post_thumbnail_id();
    $post_thumbnail_url = wp_get_attachment_image_src( $post_thumbnail_id,'smallfeature' ); ?><a href="<?php the_permalink();?>" target="_blank"><img src="<?php echo $post_thumbnail_url[0];?>" alt=" <?php the_title();?>" height="140" width="140" class="thumblist" /></a><br /><a href="<?php the_permalink();?>"><?php $trunctitle=get_the_title();echo substr($trunctitle,0,20);?>...</a></li><?php endwhile;?> </ul><div class="furtherlink"><a href="http://www.aplacetolovedogs.com/tag/quotes/" title="More of Your Dog Quotes">More Quotes...</a></div></div>

        </div></div>

Related posts

Leave a Reply

1 comment

  1. Rather than querying for a single item and then querying again for 6 more excluding the first, you can just do a single query for 7 items and selectively output markup by checking which post you’re currently outputting in the loop via $your_query_object->current_post. You can also combine that with $your_query_object->rewind_posts() to run through the same loop multiple times within a template.

    Beyond that, you can store your menu in a transient so you’re not doing all those queries on every request. I always recommend people use a caching plugin as well, like W3TC or similar.