Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Wordpress

WordPress: WP_Query Best Practices to Avoid Performance Pitfalls

- 31.05.26 - ErcanOPAK

⚡ Don’t Let WP_Query Kill Your Database

WP_Query is powerful but dangerous. Bad queries = slow site. Learn best practices for performance.

📝 Do’s and Don’ts

// ❌ DON'T: Query inside loop
while (have_posts()) {
    the_post();
    $related = new WP_Query(['post_type' => 'post', 'posts_per_page' => 5]);
    // Runs 10 times = 10 extra queries!
}

// ✅ DO: Query once, use caching
$related = new WP_Query(['post_type' => 'post', 'posts_per_page' => 50]);
$related_posts = $related->posts;

// ❌ DON'T: Query all posts without limit
$query = new WP_Query(['posts_per_page' => -1]); // Loads everything!

// ✅ DO: Limit results
$query = new WP_Query(['posts_per_page' => 50]);

// ❌ DON'T: No cache for expensive queries
$recent = new WP_Query($args);

// ✅ DO: Cache results with transients
$recent = get_transient('recent_posts');
if (false === $recent) {
    $recent = new WP_Query($args);
    set_transient('recent_posts', $recent, HOUR_IN_SECONDS);
}

🎯 Optimize Query Parameters

$args = [
    'posts_per_page' => 10,
    'no_found_rows' => true,           // Skip pagination calc (faster)
    'update_post_meta_cache' => false, // Skip meta cache (if not needed)
    'update_post_term_cache' => false, // Skip term cache (if not needed)
    'fields' => 'ids',                 // Only get IDs (much faster)
    'cache_results' => true,           // Cache query results
];

// Only get what you need
$ids = get_posts($args); // Returns array of IDs only

// Then get specific post data if needed
foreach ($ids as $id) {
    $title = get_the_title($id);
}

✅ Use wp_reset_postdata()

  • Always call wp_reset_postdata() after custom WP_Query loops
  • Prevents conflicts with main query
  • Use wp_reset_query() if you used query_posts() (avoid query_posts!)

“Homepage did 50+ queries. Added ‘no_found_rows’ and ‘update_post_meta_cache’ → queries dropped to 15. Page load halved. Tiny changes, huge impact.”

— WordPress Consultant

Related posts:

WordPress Speed Hack: How Lazy Loading Images Cuts Page Load Time in Half

WordPress: Transitioning to Headless Architecture with REST API

WordPress Login Loop Behind CDN or Proxy

Post Views: 3

Post navigation

Photoshop: Use Vanishing Point to Edit in Perspective
Kubernetes: Use VPA to Automatically Adjust CPU/Memory Requests

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

June 2026
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« May    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)
  • Add Constraint to SQL Table to ensure email contains @ (580)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (579)
  • Average of all values in a column that are not zero in SQL (538)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (505)
  • Find numbers with more than two decimal places in SQL (454)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files
  • Ajax: Add Custom Headers to Fetch Requests
  • JavaScript: Use console.table to Display Arrays as Tables
  • HTML: Use Spellcheck Attribute to Enable Browser Spell Check
  • CSS: Use user-select to Prevent Text Selection
  • Windows 11: Use Snipping Tool for Instant Screenshots

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com