Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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