Hey,
Things look to be going quite nicely now. Quite a speed boost I have got with things. But 1 things I noticed. I installed W3 Total Cache, and the site is flying now most of the time – when a spike in traffic hits we still slow down. The W3 Total Cache showed that the home page had 331 database queries to load it…that’s a massive amount! I noticed that our post page had 90+! Madness!! But then I realised I had built a slideshow at the top of the page – takes a random 20 items from any post marked as “Featured”. And iterates round them. This added somthing like 40 queries of the database onto the page – 2 per page taken from the database…here’s my code:
<?php $temp_query = clone $wp_query; ?>
<?php query_posts('category_name=Featured&orderby=rand'); ?>
<?php $count = $wp_query->post_count; ?>
<div id="slideshow-body" style="width: <?php echo $count * 214 ?>px;float:left;height: 60px; position:absolute;">
<?php while (have_posts()) : the_post(); ?>
<div class="item" style="width: 214px;float:left;height: 60px;">
<div class="image" style="width:56px;float:left;">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" onclick="_gaq.push(['_trackEvent', 'slideshow', 'viewed', '<?php the_title(); ?>']);">
<?php the_post_thumbnail( array(50,50), array('style' => 'padding:2px; border: solid 1px #ddd;') ) ?>
</a>
<?php } else {
putThumbnailForExcerpts();
} ?>
</div><!-- END IMAGE -->
<div class="link" style="width:150px;padding: 0 0 0 4px; float:left;">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" onclick="_gaq.push(['_trackEvent', 'slideshow', 'viewed', '<?php the_title(); ?>']);"><?php
$title = the_title('','',FALSE);
$len = strlen($title);
if($len > 65)
echo substr($title, 0, 65).'...';
else
echo $title;
?></a>
</div>
</div><!-- END ITEM -->
<?php endwhile; ?>
Why is it the database get’s hit so often when I set up my query in query_posts. Should it not get what I ask from the database in 1 call and store it in 1 object that get’s itterated around? It seems to go back to the database for each loop!!
And am sure this is correct as I have read about WordPress’s “loop” so could it just be the way WordPress is built that it will do this for each query and loop?