About design and nearby

WP_Query and WordPress site speed loading

Posted: November 24th, 2020 | Author: | Filed under: Development, Tips and Tricks | Tags: , , , | No Comments »

You can find so many articles online titled “How to Optimize your website powered by WordPress”. I’d like, however, show you one more advanced solution how a developer can optimize a theme.

I usually make my own queries using the WP_Query class.

For example, you may want to get the 6 latest posts and display them on a page:

  1. <?php
  2.  $args = array(
  3.   'post_type'      => 'post',
  4.   'post_status' => 'publish',
  5.   'order'          => 'DESC',
  6.   'orderby'        => 'date',
  7.   'posts_per_page' => '6'
  8.   );
  9.   $parent = new WP_Query( $args );
  10.   ?>

This query is correct .. on the first sight, however it actually runs  extra database queries to get your results.

How the meta caching works for normal queries:

If the update_post_meta_cache parameter to the WP_Query is not set to false, then after the posts are retrieved from the DB, then the update_post_caches function will be called, which in turn calls update_postmeta_cache.

The update_postmeta_cache function is a wrapper for update_meta_cache, and it essentially calls a simple SELECT with all the ID’s of the posts retrieved. This will have it get all the postmeta, for all the posts in the query, and save that data in the object cache (using wp_cache_add).

When you do something like get_post_custom(), it’s checking that object cache first. So it’s not making extra queries to get the post meta at this point. If you’ve gotten the post in a WP_Query, then the meta is already in memory and it gets it straight from there.

(Thanks a lot to Dan Smart)

So if you don’t need the postmeta for a query, that you can save time by setting the ‘update_post_meta_cache’ to false.

Here is how to optimize WP_Query to make less queries in the database and runs faster:

  1. <?php
  2.  $args = array(
  3.   'post_type'      => 'post',
  4.   'post_status' => 'publish',
  5.   'order'          => 'DESC',
  6.   'orderby'        => 'date',
  7.   'posts_per_page'      => '6',
  8.   'update_post_meta_cache' => false, // to do not run query for post meta
  9.   'update_post_term_cache' => false,  // to do not run query for terms, remove if terms required
  10.   'ignore_sticky_posts' => true,  // to ignore sticky posts  
  11.   'no_found_rows' => true  // to do not count posts – remove if pagination required
  12.   );
  13.   $parent = new WP_Query( $args );
  14.   ?>

The optimized query has the same options as the first variant.

I added the code above to my 3 custom queries placed at the index page.

Before optimization the page score according to the LightHouse tool was:

And after:

Rather better, right? :)

Share Button

Check Related Posts:


Leave a Reply


  • 7 − six =

Looking for a Freelance UX & UI designer for your project? I’m ready to jump onboard!

Let's discuss your project now