0

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

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.'%');

$wpdb->query($query);

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

$mail = $wpdb->esc_like($mail);
5
  • And yet, of course you should always do proper escaping - hoping that user input is nice is rather risky ;) – Krzysiek Dróżdż Aug 28 '18 at 17:49
  • @KrzysiekDróżdż before fire that kind of query I do all the controls for sure – Pierpaolo Ercoli Aug 28 '18 at 20:57
  • @jdp tomorrow I will try! Thanks a lot! I will tell u here. – Pierpaolo Ercoli Aug 28 '18 at 20:58
  • @jdp seems to not work your solution – Pierpaolo Ercoli Aug 29 '18 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 Aug 30 '18 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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