Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

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: Fix a Multisite Network Where New Subsites Keep Inheriting the Wrong Default Theme

WordPress: Fix a Multisite Network Where New Subsites Keep Inheriting the Wrong Default Theme

Load Plugins Only on Needed Pages

Load Plugins Only on Needed Pages

WordPress: Stop Comment Spam Instantly Without Installing a Heavy Plugin

WordPress: Stop Comment Spam Instantly Without Installing a Heavy Plugin

Views: 13

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 *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

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

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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