Performance: Why custom queries retrieve specific data
Custom queries affect how quickly and efficiently WordPress fetches and displays specific data from the database, impacting page load speed and responsiveness.
$args = array('post_type' => 'post', 'posts_per_page' => 5, 'category_name' => 'news'); $query = new WP_Query($args); while ($query->have_posts()) { $query->the_post(); // display post }
$args = array('post_type' => 'post', 'posts_per_page' => -1); $query = new WP_Query($args); while ($query->have_posts()) { $query->the_post(); // display post }
| Pattern | Database Load | Data Fetched | Server Processing | Verdict |
|---|---|---|---|---|
| Unfiltered query fetching all posts | High (full table scan) | Large (all posts) | High (processing many posts) | [X] Bad |
| Filtered query with limits and category | Low (indexed search) | Small (5 posts) | Low (processing few posts) | [OK] Good |
$args = ['author' => 5, 'posts_per_page' => 3];
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . '
';
}$args = ['category_name' => 'news'];
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . '
';
}Pass the test and this pattern moves unknown