as per title I need to use LIKE operator to delete a row on db programmatically with $wpdb.

I wanna use $wpdb->delete to remove an occurrency but I don't know how to set the LIKE operator because in WHERE it requires an array that assign a single value or another array.

I ask you if this syntax is correct.

$wpdb->delete('wp_posts', array('post_type' => 'flamingo_inbound', 'post_content' => '%'.$mail.'%'));

Thanks in advance

1 Answer 1


According to the Codex, the delete method will not accept the LIKE operator. You'll be better off just using the query method.

$query = $wpdb->prepare("DELETE FROM $wpdb->posts WHERE post_type = 'flamingo_inbound' AND post_content LIKE %s", '%'.$mail.'%');


Hopefully $mail doesn't have a % or _ character otherwise you need to escape it before you build $querylike so:

$mail = $wpdb->esc_like($mail);
  • And yet, of course you should always do proper escaping - hoping that user input is nice is rather risky ;) Commented Aug 28, 2018 at 17:49
  • @KrzysiekDróżdż before fire that kind of query I do all the controls for sure Commented Aug 28, 2018 at 20:57
  • @jdp tomorrow I will try! Thanks a lot! I will tell u here. Commented Aug 28, 2018 at 20:58
  • @jdp seems to not work your solution Commented Aug 29, 2018 at 7:22
  • @Jacob Peattie ..thanks for the edits. @Pierpaolo Ercoli ...check the edited query string. Do a var_dump on $query and see what the query looks like.
    – jdp
    Commented Aug 30, 2018 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.