0
0
Wordpressframework~20 mins

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

Choose your learning style9 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.