The Problem

WordPress uses an object cache to store database query results in memory. When a persistent object cache (Redis, Memcached) is used, cache entries may not be invalidated properly after content updates, causing users to see stale data.

Symptoms

  • Updated posts show old content on the frontend
  • Changed settings do not take effect
  • User profile updates are not reflected
  • Cache clearing fixes the issue temporarily
  • Works for admins but not regular users

Real Error Scenario

```bash # Update a post wp post update 123 --post_title="New Title" Success: Updated post 123.

# But the frontend still shows "Old Title" curl https://example.com/?p=123 | grep "<title>" # Output: <title>Old Title</title> ```

How to Fix It

Fix 1: Flush Object Cache After Updates

```bash # Flush the object cache wp cache flush

# Or specific group wp cache delete 'post-123' 'posts' ```

Fix 2: Hook Into Save Actions

php
// functions.php
function invalidate_custom_cache($post_id) {
  // Clear specific cache entries when post is saved
  wp_cache_delete($post_id, 'posts');
  wp_cache_delete('all_posts', 'custom');
  wp_cache_delete('related_posts_' . $post_id, 'custom');
}
add_action('save_post', 'invalidate_custom_cache');
add_action('delete_post', 'invalidate_custom_cache');

Fix 3: Check Redis/Memcached Connection

```bash # Redis redis-cli ping # Should return: PONG

# Check Redis keys for WordPress redis-cli KEYS "wp_*" | head -20

# Count keys redis-cli DBSIZE

# Flush WordPress keys only redis-cli KEYS "wp_*" | xargs redis-cli DEL ```

Fix 4: Verify Cache Plugin Configuration

```php // wp-config.php for Redis object cache define('WP_REDIS_HOST', '127.0.0.1'); define('WP_REDIS_PORT', 6379); define('WP_REDIS_DATABASE', 0); define('WP_REDIS_MAXTTL', 86400);

// For Memcached define('WP_CACHE_KEY_SALT', 'my-site-'); // Unique prefix per site ```

Fix 5: Debug Cache Hit/Miss Ratio

```php // Enable cache debugging define('WP_DEBUG', true);

// Add to monitor cache performance add_action('shutdown', function() { global $wp_object_cache; if (method_exists($wp_object_cache, 'stats')) { error_log(print_r($wp_object_cache->stats(), true)); } }); ```

Fix 6: Use Versioned Cache Keys

```php // Instead of: $cached = wp_cache_get('homepage_data');

// Use versioned keys: $version = get_option('cache_version', 1); $cached = wp_cache_get("homepage_data_v{$version}");

// To invalidate all: update_option('cache_version', get_option('cache_version', 1) + 1); ```