Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I acquainted a client who designed his own fashion magazine site. My company handed the project to me to make it with WordPress. When I was making the complete template for the site, I found his complete thought: and it's completely eerie for me; because the design for the home page had many more blocks, not less than 20 blocks, and each of the blocks demands a new WP_Query() (and just now from this thread I was known that I's right making new WP_Query() for each of the blocks).

And the final product was damn slow - as I suspected.

I'm not a WordPress master, so as I understood WP_Query() is whenever it's called, it hits the db with it's query and retrieve the data as per the query. So in this site, in the Home Page it's hitting 20 times and then retrieving the data and that makes it slower.

With this scenario, I's thinking of some Speeding Up solutions, like:

  • Separating the loop for each of the posts with a common function so that it loads a single time with different parameters each of the time
  • Some basic CSS things to speed up the layout
  • Separating the common things into different files and include them with get_template_part(), so that the common thing will load a single time and echo multiple times wherever it's necessary
  • Using LazyLoad to the site images to let 'em not hamper the site speed when the content and layout is important first
  • Caching the site's content
  • WP Smush.it the images
  • If possible and doesn't create conflict then using the .js compression

With these thoughts, I may be wrong with much of their concepts, but I found them logically good.

So the ultimate questions are:

  • What can be done further to speed up a site with multiple queries (i.e. 20 different queries)?
  • Or, is there any better solution instead of using WP_Query() for each of the queries?
share|improve this question
1  
1st point in list don't change anything: if the common function call a WP_Query, the db queries are exactly the same. The other points can speed up the site, but aren't related to the queries, so do them if you want, but are valid things indifferently for a page with 1 or 20 queries... The only query-related point in your list is "Caching the site's content": a good point for sure, but in my experience, the queries in a page can be reduced in number merging them and using logic to separate posts and show them in right place. 20 queries are really an high number for one single page. –  G. M. Sep 13 '13 at 9:34
add comment

2 Answers

up vote 2 down vote accepted

save queries into transients for a specific time by using set_transient(); and retrieve saved query using get_transient(); this will reduce your site's loading time too much, as you stated you are using 20 queries on a page then you need to set different transients expiration time so all transients don't expire at once.

example usage:

$my_query = get_transient('my_query');

if (!$my_query) {
    // run query
    $args = array(
        // query args
    );

    $my_query = new WP_Query($args);
            // set transient for 3 hours
    set_transient('my_query', $my_query, 60*60*3);
}

   // run your loop
   if ($my_query->have_posts()) :
        while ($my_query->have_posts()) :
           $my_query->the_post();
           // do your stuff
        endwhile;
   endif;
share|improve this answer
add comment

You must use cache systems.without caching, your site will be slowly.use APC or memcache for increase speed of your home page.

or use redis.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.