Bird
Raised Fist0
Wordpressframework~30 mins

Caching strategies in Wordpress - Mini Project: Build & Apply

Choose your learning style10 modes available
Implementing Basic Caching Strategies in WordPress
📖 Scenario: You are building a WordPress site that loads a list of recent blog posts. To improve performance and reduce server load, you want to cache the list of posts so the site does not query the database every time a visitor loads the page.
🎯 Goal: Build a simple caching mechanism in WordPress using the Transients API to store and retrieve the list of recent posts.
📋 What You'll Learn
Create a function to get recent posts and cache them using transients
Set a cache expiration time of 1 hour
Retrieve cached posts if available instead of querying the database
Clear the cache when a new post is published
💡 Why This Matters
🌍 Real World
Caching is essential for speeding up WordPress sites by reducing database queries and server load, improving user experience.
💼 Career
Understanding caching strategies is important for WordPress developers to build fast, scalable websites and optimize performance.
Progress0 / 4 steps
1
Create a function to get recent posts
Create a function called get_recent_posts() that uses get_posts() to retrieve the 5 most recent posts and returns them.
Wordpress
Hint

Use get_posts() with numberposts set to 5 inside your function.

2
Add a cache expiration time variable
Add a variable called $cache_expiration and set it to 3600 (seconds) inside the get_recent_posts() function, before retrieving posts.
Wordpress
Hint

Set $cache_expiration = 3600; inside the function before fetching posts.

3
Use transients to cache and retrieve posts
Modify the get_recent_posts() function to first check if a transient called 'recent_posts_cache' exists using get_transient(). If it exists, return the cached posts. If not, fetch posts with get_posts(), store them in the transient with set_transient() using the $cache_expiration time, then return the posts.
Wordpress
Hint

Use get_transient() to check cache and set_transient() to save posts with expiration.

4
Clear cache when a new post is published
Add an action hook using add_action() to call a function named clear_recent_posts_cache() on the 'save_post' hook. Create the clear_recent_posts_cache() function that deletes the transient 'recent_posts_cache' using delete_transient().
Wordpress
Hint

Use add_action('save_post', 'clear_recent_posts_cache') and define clear_recent_posts_cache() to delete the transient.

Practice

(1/5)
1. What is the main purpose of caching in WordPress?
easy
A. To speed up the website by saving copies of pages or data
B. To increase the size of the website database
C. To delete old posts automatically
D. To change the website theme dynamically

Solution

  1. Step 1: Understand caching concept

    Caching stores copies of pages or data to avoid repeated processing.
  2. Step 2: Identify caching benefit

    By saving copies, the website loads faster for visitors.
  3. Final Answer:

    To speed up the website by saving copies of pages or data -> Option A
  4. Quick Check:

    Caching = Speed up site [OK]
Hint: Caching saves copies to load pages faster [OK]
Common Mistakes:
  • Thinking caching deletes content
  • Believing caching increases database size
  • Confusing caching with theme changes
2. Which of the following is the correct way to clear the WordPress cache using a plugin?
easy
A. Change the site URL in settings
B. Click the 'Clear Cache' button in the caching plugin settings
C. Delete all posts from the dashboard
D. Deactivate all plugins except caching

Solution

  1. Step 1: Identify cache clearing method

    Caching plugins provide a 'Clear Cache' button to remove saved copies.
  2. Step 2: Recognize incorrect options

    Deleting posts, changing URL, or deactivating plugins do not clear cache properly.
  3. Final Answer:

    Click the 'Clear Cache' button in the caching plugin settings -> Option B
  4. Quick Check:

    Clear cache = Use plugin button [OK]
Hint: Use plugin's clear cache button to refresh content [OK]
Common Mistakes:
  • Deleting posts thinking it clears cache
  • Changing site URL to fix cache
  • Deactivating unrelated plugins
3. Consider this code snippet in a WordPress theme's functions.php file:
function get_cached_data() {
  $cache_key = 'my_data';
  $data = wp_cache_get($cache_key);
  if ($data === false) {
    $data = 'Expensive data';
    wp_cache_set($cache_key, $data, '', 3600);
  }
  return $data;
}

echo get_cached_data();

What will this code output on the first call?
medium
A. An error message
B. false
C. null
D. Expensive data

Solution

  1. Step 1: Check cache retrieval

    On first call, wp_cache_get returns false because cache is empty.
  2. Step 2: Set and return data

    Since cache is empty, 'Expensive data' is set and returned.
  3. Final Answer:

    Expensive data -> Option D
  4. Quick Check:

    First call returns new data [OK]
Hint: First cache get returns false, so data is set and returned [OK]
Common Mistakes:
  • Assuming cache returns null instead of false
  • Expecting error on missing cache
  • Thinking cache returns old data on first call
4. You added caching to your WordPress site, but after updating content, visitors still see old pages. What is the likely problem?
medium
A. The site URL is incorrect
B. The theme is incompatible with WordPress
C. Cache was not cleared after content update
D. The database is corrupted

Solution

  1. Step 1: Understand caching effect

    Caching saves old page copies to speed up loading.
  2. Step 2: Identify update issue

    If cache is not cleared after content changes, old pages show to visitors.
  3. Final Answer:

    Cache was not cleared after content update -> Option C
  4. Quick Check:

    Old pages = Cache not cleared [OK]
Hint: Always clear cache after content updates [OK]
Common Mistakes:
  • Blaming theme for caching issues
  • Assuming database corruption causes old pages
  • Thinking site URL affects caching
5. You want to improve your WordPress site's speed by caching database query results for 10 minutes. Which approach is best?
hard
A. Use wp_cache_set() with a 600-second expiration after fetching query results
B. Disable all caching plugins and rely on browser cache only
C. Manually save query results in a text file and read it each time
D. Increase PHP memory limit to store all queries in RAM

Solution

  1. Step 1: Identify caching method for query results

    wp_cache_set() stores data in WordPress object cache with expiration time.
  2. Step 2: Set expiration for 10 minutes

    600 seconds equals 10 minutes, so setting expiration to 600 caches results properly.
  3. Step 3: Evaluate other options

    Disabling plugins loses caching benefits; manual file saves are inefficient; increasing memory doesn't cache queries automatically.
  4. Final Answer:

    Use wp_cache_set() with a 600-second expiration after fetching query results -> Option A
  5. Quick Check:

    Cache query with wp_cache_set + 600s [OK]
Hint: Use wp_cache_set with expiration for timed caching [OK]
Common Mistakes:
  • Disabling caching plugins thinking it helps
  • Trying manual file caching instead of built-in cache
  • Increasing memory without caching logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard