Skip to content

Instantly share code, notes, and snippets.

@apsolut
Forked from mishterk/wp-query-efficiency-args.php
Created September 1, 2021 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apsolut/07a72460d53cec3967fd3e6ef535da40 to your computer and use it in GitHub Desktop.
Save apsolut/07a72460d53cec3967fd3e6ef535da40 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