Performance tips when using WP_Query

Posted in Snippets

The WP_Query class is very powerful and can be used in many ways.

Here is a quick tip to enhance the performance. Read the inline comments!

$args = [
  'posts_per_page'  => 500, // Don't use -1. Instead, set a high number if you need to get a lot of posts.
  'cache_results'   => false, // Just don't do this. Thanks.

  /**
   * Performance fix.
   *
   * @link https://10up.github.io/Engineering-Best-Practices/php/#performance
   */
  'no_found_rows'          => true,  // Useful when pagination is not needed.
  'update_post_meta_cache' => false, // Post meta will not be utilized.
  'update_post_term_cache' => false, // Taxonomy terms will not be utilized.
];

$query = new WP_Query($args);

Be sure to read every bit of this great piece of work. https://10up.github.io/Engineering-Best-Practices

You’ve already read it? Read it again!

Leave a Comment