Performance: Custom post type queries
MEDIUM IMPACT
This affects page load speed by controlling how many database queries run and how much data is fetched for custom post types.
<?php $paged = get_query_var('paged') ?: 1; $query = new WP_Query(array('post_type' => 'product', 'posts_per_page' => 10, 'paged' => $paged)); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); the_title(); } } wp_reset_postdata(); ?>
<?php $posts = get_posts(array('post_type' => 'product')); foreach ($posts as $post) { setup_postdata($post); the_title(); } wp_reset_postdata(); ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetching all posts without limits | N/A (server-side) | N/A | Blocks initial paint due to slow HTML generation | [X] Bad |
| Paginated query with limits | N/A (server-side) | N/A | Faster HTML generation enables quicker paint | [OK] Good |
| Query without caching | N/A (server-side) | N/A | Slower server response delays paint | [!] OK |
| Query with caching | N/A (server-side) | N/A | Fast server response improves paint timing | [OK] Good |