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 Transients: Cache Data Without a Plugin

- 19.03.26 - ErcanOPAK

⚡ Built-in Caching System

API call taking 2 seconds? Database query slow? Cache it with Transients API. No plugin needed.

Basic Usage

// Get from cache
$data = get_transient('my_cached_data');

// Not in cache? Get fresh data
if (false === $data) {
    $data = expensive_api_call();
    
    // Store for 1 hour (3600 seconds)
    set_transient('my_cached_data', $data, HOUR_IN_SECONDS);
}

return $data;

Real Example: Cache WooCommerce Products

function get_featured_products() {
    $transient_key = 'featured_products_v1';
    $products = get_transient($transient_key);
    
    if (false === $products) {
        // Expensive query
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 10,
            'meta_query' => array(
                array('key' => '_featured', 'value' => 'yes')
            )
        );
        $products = new WP_Query($args);
        
        // Cache for 12 hours
        set_transient($transient_key, $products, 12 * HOUR_IN_SECONDS);
    }
    
    return $products;
}

// Delete cache when product updated
add_action('save_post_product', function() {
    delete_transient('featured_products_v1');
});

⏱️ Time Constants

  • MINUTE_IN_SECONDS = 60
  • HOUR_IN_SECONDS = 3600
  • DAY_IN_SECONDS = 86400
  • WEEK_IN_SECONDS = 604800
  • MONTH_IN_SECONDS = 2592000
  • YEAR_IN_SECONDS = 31536000

“Homepage was hitting Instagram API 50 times per minute. Added transient cache (30 min expiry). Server load dropped 90%. Zero plugins.”

— WordPress Developer

Related posts:

Solution for "utf8mb4 requires a newer client library" error in WordPress

WordPress: Enable Maintenance Mode Without Plugins Using One Line of Code

Disable REST API Plugin

Post Views: 5

Post navigation

WordPress Query Monitor: Debug Performance Issues in Real Time
Kubernetes: Use kubectl port-forward to Debug Services Locally

Leave a Reply Cancel reply

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

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

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

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

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

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

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