How to use mysql LIKE with wpdb?

as per title I need to use </code>LIKE<code> 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

New User Asked on September 19, 2018 in Custom Development.
Add Comment
1 Answer(s)

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);

 

New User Answered on September 19, 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service. | Learn how to add/edit code.