Home / CMS & DEV / Optimize SQL queries
CMS & DEV
Optimize SQL queries for your WordPress themes
A poorly optimized theme can multiply SQL queries by ten. WP_QUERY well configured and the transients drastically reduce the database load.
Avoid redundant requests with WP_QUERY
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 10,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
]);
Caching with the Transients
function get_featured_products() {
$cached = get_transient('featured_products');
if ($cached !== false) return $cached;
$products = new WP_Query(['post_type' => 'product', 'meta_key' => 'featured']);
set_transient('featured_products', $products->posts, HOUR_IN_SECONDS);
return $products->posts;
}
Tip: Enable Query Monitor in the development environment to visualize exactly what queries are slowing down your theme.
frequently asked questions
no_found_rows breaks pagination?
Yes, use it only on queries without pagination; Otherwise keep Found_Rows active.
Do transients survive a Redis cache object?
Yes, with a persistent cache object plugin, transients are automatically stored in memory rather than in base.