3

I am wondering if this is possible?

$wpdb->delete(
    'table_name',
    array('id' => array(1, 2, 3)),
    array('%d')
);

So, in this situation, it should remove 3 rows at once, and call the database only 1 time. I have a lot of deletions that could be possible with my script and would rather it just perform the deletion once, instead of having to loop through all of the ids and do a $wpdb->delete on each one individually. Is this possible? Seems like it should be...

3
  • Have you tried it? Or better yet, looked it up?
    – Tim Malone
    May 22 '16 at 9:37
  • Yes, I have looked at the function and didn't seem possible. But honestly, this should be possible to do, as it's not much of a difference and would make sense to have in this function. May 22 '16 at 17:34
  • I don't understand why the downvote here... maybe whoever did it can help me to understand why? Why the ignorance of asking if I looked it up? Ofcourse I did, and thought I would ask anyways. Isn't that what this site is for? May 22 '16 at 17:44
11

No, wpdb::delete does not handle anything other than WHERE field = X. You can just use the query method instead:

$ids = implode( ',', array_map( 'absint', $ids ) );
$wpdb->query( "DELETE FROM table_name WHERE ID IN($ids)" );
6
  • Thanks, I'm aware of the alternatives. I don't see the relevance of using absint since that would only be relevant for UNSIGNED columns, and might not be the case. But thanks for your alternate method example just the same. May 22 '16 at 17:43
  • Just demonstrating a level of sanitization; I doubt your ID's will be hard-coded? May 22 '16 at 17:45
  • Yes, ids are not hardcoded, they are within an array, so this is a good example, just not using absint I suppose. Thanks anyways. May 22 '16 at 18:02
  • Ok then just use intval instead May 22 '16 at 18:02
  • 3
    If you want this functionality, submit a trac ticket - that's the beauty of open source, it's built by all of us. May 22 '16 at 18:04

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.