It's my fault. Got the solution after @Otto's comment: > Do you actually have a duplicate key here? What is the structure of > user_req and what are the keys and indexes? Here's how my SQL query should be: INSERT INTO {$wpdb->prefix}user_req ( user_id, post_id ) VALUES ( %d, %d ) WHERE NOT EXISTS ( SELECT * FROM {$wpdb->prefix}user_req.user_id = user_id AND {$wpdb->prefix}user_req.post_id = post_id ) Fixed now. Thanks to Otto.