5

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? Commented May 22, 2016 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. Commented May 22, 2016 at 17:34
  • 1
    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? Commented May 22, 2016 at 17:44

1 Answer 1

12

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. Commented May 22, 2016 at 17:43
  • Just demonstrating a level of sanitization; I doubt your ID's will be hard-coded? Commented May 22, 2016 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. Commented May 22, 2016 at 18:02
  • Ok then just use intval instead Commented May 22, 2016 at 18:02
  • 4
    If you want this functionality, submit a trac ticket - that's the beauty of open source, it's built by all of us. Commented May 22, 2016 at 18:04

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.