Optimizing a Large Directory Page with numerous Query_posts
-
I’m working on this page: http://myalumnibar.com/schools/
the goal is to create an alphabetical listing of schools by getting a separate list for each letter of the alphabet.I want to get all the posts by letter individually so that I can separate them as lists with individual headings and eventually make the entire thing a javascript menu. The current code produces exactly the results I am looking for, but is horribly inefficient and results in incredibly long load times. I’m looking for suggestions to optimize this page somehow so I don’t have to make 26 individual queries.
Currently, this is the function I have setup to run the query for each letter:
<?php function AlphaList($first_char) { echo "<ul class=\"schools\">"; global $post; //Query posts that are children of the specific 'Schools' category, exclude those in the Trivia category ($SchoolscatID and $triviacatID are globally set in a separate file $myposts = query_posts('numberposts=-1&child_of=' . $schoolscatID . '&cat=-' . $triviacatID . '&orderby=title&order=ASC') foreach($myposts as $post) : //get('School_name') is the custom field created by Flutter (plugin) that specifies the school's name $schooltitle = get('School_name'); //$schoolletter gets the first letter of the School Name from each post $schoolletter = substr ($schooltitle, 0, 1); //If the Schoolletter is the same as the functon paramater, then get the name of the category each post belongs to which is a child of the main 'Schools' category (cat id= 33); If the letters dont match, then dont do anything. if ($schoolletter == $first_char) { $catlink =""; foreach((get_the_category()) as $category) { if ($category->category_parent == 33) { $catlink .= $category->slug; }} //For these posts, display a link that takes you to the category archive page echo "<li><a href=\"" . get_bloginfo('url') . "/category/schools/" . $catlink . "\">" .$schooltitle ." </a> </li>"; }else { continue; } endforeach; //Done with the posts now echo "</ul>"; } ?>And then every letter is separated like this:
<h3 class="menuheader expandable">A</h3> <ul class="categoryitems"><?php AlphaList("A"); ?> </ul> <h3 class="menuheader expandable">B</h3> <ul class="categoryitems"><?php AlphaList("B"); ?> </ul> <h3 class="menuheader expandable">C</h3> <ul class="categoryitems"><?php AlphaList("C"); ?> </ul>Anyone have a suggestion for how to rewrite this to help speed up the page significantly?
Thanks!
-
Focusing solely on knocking down the 26 queries to one, you could code:
<h3 class="menuheader expandable">A</h3> <ul class="categoryitems"><?php $myposts = query_posts('numberposts=-1&child_of=' . $schoolscatID . '&cat=-' . $triviacatID . '&orderby=title&order=ASC'); AlphaList("A", $myposts); ?> </ul> <h3 class="menuheader expandable">B</h3> <ul class="categoryitems"><?php AlphaList("B", $myposts); ?> </ul> <h3 class="menuheader expandable">C</h3> <ul class="categoryitems"><?php AlphaList("C", $myposts); ?> </ul>and remove the query from the AlphaList function. Of course, you also have to add $myposts to the AlphaList function’s parameter definitions.
The topic ‘Optimizing a Large Directory Page with numerous Query_posts’ is closed to new replies.