Skip to content

Instantly share code, notes, and snippets.

@mishterk
Created June 24, 2021 23:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mishterk/6bea2b6226fafac84fa44190ec14ac5d to your computer and use it in GitHub Desktop.
Save mishterk/6bea2b6226fafac84fa44190ec14ac5d to your computer and use it in GitHub Desktop.
Some handy considerations when running WP_Query to speed up the query depending on the requirements. This demonstrates using specific post ID's from ACF custom database tables in conjunction with some built in WP_Query args to save on internal queries.
<?php
$query = new WP_Query([
// Standard query args. Using post__in can be much faster. If searching ACF custom
// database tables data, plugin the found post IDs in here.
'post_type' => 'post',
'post__in' => [1,2,3], // array of post IDs
// Optional args to improve performance. Use these to cut down on internal
// complexity and make the overall internal SQL faster.
// Don't need total found rows?
// Set this to true to save on additional queries.
'no_found_rows' => true,
// Don't need WordPress to run the meta query for all meta data for found posts?
// Set this to false to save on additional queries.
'update_post_meta_cache' => false,
// Don't need to worry about taxonomies for found posts?
// Set this to false to save on additional queries.
'update_post_term_cache' => false,
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment