Bird
Raised Fist0
Wordpressframework~20 mins

Direct database queries (wpdb) in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
wpdb Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this wpdb query return?
Consider this WordPress wpdb query:
global $wpdb;
$result = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");
echo $result;

What will be the output if there are 10 published posts in the database?
Wordpress
global $wpdb;
$result = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");
echo $result;
AError: Undefined variable
BArray with 10 elements
CNULL
D10
Attempts:
2 left
💡 Hint
get_var returns a single value from the first column of the first row.
📝 Syntax
intermediate
2:00remaining
Which wpdb query syntax is correct to safely insert data?
You want to insert a new user with name and email into a custom table using wpdb. Which option uses the correct wpdb method and syntax to safely insert data?
A$wpdb->insert('wp_custom_users', ['name' => $name, 'email' => $email]);
B$wpdb->query("INSERT INTO wp_custom_users (name, email) VALUES ('$name', '$email')");
C$wpdb->insert('wp_custom_users', $name, $email);
D$wpdb->get_results("INSERT INTO wp_custom_users (name, email) VALUES (?, ?)", [$name, $email]);
Attempts:
2 left
💡 Hint
Look for the method designed for inserting data with automatic escaping.
🔧 Debug
advanced
2:00remaining
Why does this wpdb query cause an error?
Look at this code snippet:
global $wpdb;
$user_id = 5;
$result = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}users WHERE ID = $user_id");
echo $result->user_login;

Why might this cause a PHP error?
Wordpress
global $wpdb;
$user_id = 5;
$result = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}users WHERE ID = $user_id");
echo $result->user_login;
Aget_row returns an array, so ->user_login is invalid.
BThe query syntax is invalid because variables cannot be inside double quotes.
C$result is NULL if no user with ID 5 exists, so accessing ->user_login causes an error.
DThe $wpdb->prefix is undefined causing a fatal error.
Attempts:
2 left
💡 Hint
Check what happens if the query returns no rows.
❓ state_output
advanced
2:00remaining
What is the value of $count after this wpdb query?
Given this code:
global $wpdb;
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_author = %d", 3));

Assuming the author with ID 3 has 7 posts, what is the value of $count?
Wordpress
global $wpdb;
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_author = %d", 3));
A7
BArray with 7 elements
CError: prepare method not found
DNULL
Attempts:
2 left
💡 Hint
prepare safely inserts the number 3 into the query.
🧠 Conceptual
expert
2:00remaining
Which wpdb method should you use to get multiple rows as objects?
You want to retrieve all published posts from the database as an array of objects. Which wpdb method is the best choice?
A$wpdb->get_var("SELECT * FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");
B$wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");
C$wpdb->get_row("SELECT * FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");
D$wpdb->insert("{$wpdb->prefix}posts", ['post_status' => 'publish']);
Attempts:
2 left
💡 Hint
Think about which method returns multiple rows.

Practice

(1/5)
1. What is the main purpose of the $wpdb object in WordPress?
easy
A. To style WordPress themes with CSS
B. To create new WordPress posts automatically
C. To manage WordPress user sessions
D. To run direct SQL queries on the WordPress database safely

Solution

  1. Step 1: Understand the role of $wpdb

    $wpdb is a WordPress class that allows running SQL queries directly on the database.
  2. Step 2: Identify the correct purpose

    It is used to safely execute SQL queries, not for posts, sessions, or styling.
  3. Final Answer:

    To run direct SQL queries on the WordPress database safely -> Option D
  4. Quick Check:

    $wpdb = direct database queries [OK]
Hint: Remember: $wpdb is for database queries only [OK]
Common Mistakes:
  • Confusing $wpdb with theme or user management
  • Thinking $wpdb handles styling or sessions
2. Which of the following is the correct way to safely prepare a SQL query using $wpdb?
easy
A. $wpdb->prepare("SELECT * FROM wp_posts WHERE ID = %d", $id);
B. $wpdb->query("SELECT * FROM wp_posts WHERE ID = $id");
C. $wpdb->get_results("SELECT * FROM wp_posts WHERE ID = $id");
D. $wpdb->execute("SELECT * FROM wp_posts WHERE ID = %d", $id);

Solution

  1. Step 1: Identify the safe query method

    $wpdb->prepare() safely inserts variables into SQL queries using placeholders like %d.
  2. Step 2: Check each option

    $wpdb->prepare("SELECT * FROM wp_posts WHERE ID = %d", $id); uses prepare correctly with %d and variable $id. Others either lack preparation or use wrong methods.
  3. Final Answer:

    $wpdb->prepare("SELECT * FROM wp_posts WHERE ID = %d", $id); -> Option A
  4. Quick Check:

    Safe query uses prepare with placeholders [OK]
Hint: Use prepare with %d, %s for safe queries [OK]
Common Mistakes:
  • Inserting variables directly without prepare
  • Using wrong method names like execute
  • Not using placeholders for variables
3. What will the following code return if the wp_users table has 3 users?
$count = $wpdb->get_var("SELECT COUNT(*) FROM wp_users");
medium
A. An array of user data
B. The number 3
C. A boolean true
D. An error message

Solution

  1. Step 1: Understand get_var usage

    get_var returns a single value from the database, here the count of users.
  2. Step 2: Interpret the SQL query

    The query counts rows in wp_users, which is 3 users.
  3. Final Answer:

    The number 3 -> Option B
  4. Quick Check:

    get_var returns single value count [OK]
Hint: get_var returns one value, not arrays [OK]
Common Mistakes:
  • Expecting an array instead of a single value
  • Confusing get_var with get_results
  • Thinking it returns true/false
4. Identify the error in this code snippet:
$results = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_status = 'publish'");
foreach($results as $post) {
echo $post->post_title;
}
medium
A. Loop variable should be $results not $post
B. Incorrect method; should use query instead of get_results
C. No error; code works correctly
D. Missing $wpdb->prepare() for safe query

Solution

  1. Step 1: Check query safety

    The query uses a fixed string 'publish' so no variable injection risk, so prepare is not required here.
  2. Step 2: Analyze method and loop

    get_results returns an array of objects; looping with $post is correct. No syntax error here.
  3. Final Answer:

    No error; code works correctly -> Option C
  4. Quick Check:

    Fixed string query is safe without prepare [OK]
Hint: Use prepare when variables are in query [OK]
Common Mistakes:
  • Ignoring query preparation for dynamic inputs
  • Confusing get_results with query
  • Misusing loop variables
5. You want to update the post title of a post with ID stored in $post_id safely using $wpdb. Which code snippet correctly does this?
hard
A. $prepared = $wpdb->prepare("UPDATE wp_posts SET post_title = %s WHERE ID = %d", $new_title, $post_id); $wpdb->query($prepared);
B. $wpdb->prepare("UPDATE wp_posts SET post_title = %s WHERE ID = %d", $new_title, $post_id); $wpdb->query($prepared);
C. $wpdb->query("UPDATE wp_posts SET post_title = '$new_title' WHERE ID = $post_id");
D. $wpdb->update('wp_posts', ['post_title' => $new_title], ['ID' => $post_id]);

Solution

  1. Step 1: Understand safe update with $wpdb

    Use prepare to safely insert variables, then run query with prepared SQL.
  2. Step 2: Check each option

    $prepared = $wpdb->prepare("UPDATE wp_posts SET post_title = %s WHERE ID = %d", $new_title, $post_id); $wpdb->query($prepared); correctly prepares the query and then runs it. $wpdb->prepare("UPDATE wp_posts SET post_title = %s WHERE ID = %d", $new_title, $post_id); $wpdb->query($prepared); calls prepare but does not assign or run the result properly. $wpdb->query("UPDATE wp_posts SET post_title = '$new_title' WHERE ID = $post_id"); is unsafe. $wpdb->update('wp_posts', ['post_title' => $new_title], ['ID' => $post_id]); uses update method but misses table prefix and is not asked here.
  3. Final Answer:

    $prepared = $wpdb->prepare("UPDATE wp_posts SET post_title = %s WHERE ID = %d", $new_title, $post_id); $wpdb->query($prepared); -> Option A
  4. Quick Check:

    Prepare query, assign, then query [OK]
Hint: Assign prepare result before running query [OK]
Common Mistakes:
  • Running prepare without assigning result
  • Inserting variables directly into SQL
  • Confusing update method usage
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