📖 Scenario: You manage a WordPress website that has grown large and slow. You want to optimize the database queries to improve site speed and user experience.
🎯 Goal: Build a simple WordPress plugin that optimizes database queries by cleaning up post revisions and limiting query results.
📋 What You'll Learn
Create a function to delete old post revisions
Add a configuration variable to set the maximum number of posts per page
Write a function to limit the number of posts returned by a query
Hook the functions properly into WordPress actions
💡 Why This Matters
🌍 Real World
WordPress sites often slow down due to large numbers of post revisions and heavy queries. Cleaning revisions and limiting query results improves speed and user experience.
💼 Career
WordPress developers and site admins need to optimize database performance to maintain fast, scalable websites.
Progress0 / 4 steps
1
Create a function to delete old post revisions
Create a function called delete_old_revisions that deletes post revisions older than 30 days using wpdb and SQL DELETE query.
Wordpress
Hint
Use the global $wpdb object to run a SQL DELETE query targeting revisions older than 30 days.
2
Add a configuration variable for max posts per page
Add a variable called $max_posts and set it to 5 to limit the number of posts shown per page.
Wordpress
Hint
Define a variable $max_posts and assign it the value 5.
3
Write a function to limit posts returned by query
Create a function called limit_posts_query that accepts a $query parameter and sets $query->set('posts_per_page', $max_posts) to limit posts returned. Use the global $max_posts inside the function.
Wordpress
Hint
Use global $max_posts inside the function and check $query->is_main_query() and !is_admin() before setting posts_per_page.
4
Hook functions into WordPress actions
Use add_action to hook delete_old_revisions to 'wp_scheduled_delete' and add_action to hook limit_posts_query to 'pre_get_posts'.
Wordpress
Hint
Use add_action to connect your functions to the correct WordPress hooks.
Practice
(1/5)
1. What is the main purpose of database optimization in WordPress?
easy
A. To change the website's theme
B. To add more plugins automatically
C. To make the website load faster and run more efficiently
D. To increase the number of posts displayed
Solution
Step 1: Understand database optimization
Database optimization improves how quickly and efficiently data is accessed and stored.
Step 2: Relate to WordPress site performance
A faster database means the website loads quicker and runs smoother for visitors.
Final Answer:
To make the website load faster and run more efficiently -> Option C
Quick Check:
Database optimization = faster site [OK]
Hint: Optimization improves speed and efficiency [OK]
Hint: Always use OPTIMIZE TABLE, not just OPTIMIZE [OK]
Common Mistakes:
Omitting TABLE keyword
Assuming table doesn't exist without checking
Using DELETE instead of OPTIMIZE
5. You want to optimize your WordPress database but keep a backup first. Which sequence of commands is best to safely optimize the database?
hard
A. wp db import backup.sql && wp db optimize
B. wp db optimize && wp db export backup.sql
C. wp db reset && wp db optimize
D. wp db export backup.sql && wp db optimize
Solution
Step 1: Understand the need for backup before optimization
Backing up first ensures you can restore data if optimization causes issues.
Step 2: Analyze the command sequence
wp db export backup.sql creates a backup file, then wp db optimize optimizes the database safely.
Step 3: Check other options for correctness
wp db optimize && wp db export backup.sql optimizes before backup (risky), wp db reset && wp db optimize resets database (dangerous), wp db import backup.sql && wp db optimize imports backup before optimizing (doesn't create a new backup first).
Final Answer:
wp db export backup.sql && wp db optimize -> Option D
Quick Check:
Backup then optimize = safe practice [OK]
Hint: Always backup before optimizing database [OK]
Common Mistakes:
Optimizing before backup
Resetting database accidentally
Importing backup instead of exporting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.