Challenge - 5 Problems
wpdb Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this wpdb query return?
Consider this WordPress wpdb query:
What will be the output if there are 10 published posts in the database?
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;
Attempts:
2 left
💡 Hint
get_var returns a single value from the first column of the first row.
✗ Incorrect
The get_var method returns a single value from the database query. Since the query counts published posts and there are 10, it returns '10' as a string.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Look for the method designed for inserting data with automatic escaping.
✗ Incorrect
The insert() method takes the table name and an associative array of column => value pairs and safely escapes the data. Option A uses this correctly.
🔧 Debug
advanced2:00remaining
Why does this wpdb query cause an error?
Look at this code snippet:
Why might this cause a PHP error?
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;
Attempts:
2 left
💡 Hint
Check what happens if the query returns no rows.
✗ Incorrect
If the user with ID 5 does not exist, get_row returns NULL. Trying to access a property on NULL causes a PHP error.
❓ state_output
advanced2:00remaining
What is the value of $count after this wpdb query?
Given this code:
Assuming the author with ID 3 has 7 posts, what is the value of $count?
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));
Attempts:
2 left
💡 Hint
prepare safely inserts the number 3 into the query.
✗ Incorrect
The prepare method replaces %d with 3 safely. The query counts posts by author 3, which is 7, so get_var returns '7'.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about which method returns multiple rows.
✗ Incorrect
get_results returns an array of objects for multiple rows. get_var returns a single value, get_row returns one row, and insert adds data.