WordPress.org

Ready to get started?Download WordPress

Forums

Multiple loops in wordpress with minimal database queries (1 post)

  1. knivesOut
    Member
    Posted 4 years ago #

    Hello everyone.

    I'm using WordPress as a CMS and what I'm trying to do is create a site-map type footer that's going to list all pages within each category. An example of this would be what's going on in the footer of this website - mesa.cityofgrace.com.

    From using the "Debug Queries" plug-in from Frank Bültge, I've found out that I'm using about 14 queries per page--this is with 3 plug-ins installed.

    So what I'm doing for my site-map like footer is creating custom loops for each category, listing out all of the pages under them (like in the example Web site above). I have 5 categories in my WordPress CMS Web site with about 5-7 pages within each section/category. When I do this, the number of queries jumps to about 32 queries per page. The code I'm using us pasted below.

    I feel like I'm doing these loops incorrectly and generating unnecessary queries. Can anyone help me with the proper way to code my multiple loops?

    I'm reusing the following code 5 times in the footer for my "site map" and it's generating 18 more queries.

    <ul class="footer-lists">
    <li><a href="#" title="" class="bold">About Me</a></li>
    <?php
    $categories = array(7);
    if ($categories) {
    	$category_ids = array();
    	foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    $args=array(
    		'category__in' => $categories,
    		'showposts'=>12,
    		'caller_get_posts'=>1
    	);
    $my_query = new wp_query($args);
    	if( $my_query->have_posts() ) {
    		while ($my_query->have_posts()) {
    			$my_query->the_post();
    		?>
    <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
      <?php the_title(); ?>
      </a></li>
    <?php
    		}
    		echo '</ul>';
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic