So I'm updating the code of an old wordpress plugin(never released), and I want the database to be "clean" before importing new data otherwise data will just add up and i want it clean before every import, so that duplicates dont happen.

This is the code that i'm using but doesn't seem to work.

global $wpdb;
$wpdb->delete( $wpdb->postmeta WHERE meta_key LIKE '_cb_%' ) );
$wpdb->delete( $wpdb->posts WHERE post_type LIKE 'webcam' ) );

What's wrong with this picture?

Thanks in advance.


Look at the documentation, $wpdb->delete() takes at least two parameters, the table name and the Where-conditions as an array(). LIKE isn't supported for delete imho, so you'll need to use ->query

global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE '_cb_%'" );
$wpdb->query( "DELETE FROM $wpdb->posts WHERE post_type LIKE 'webcam'" );

I assume the double closing parentheses and missing quotes were just from copy pasting?

  • Tried it so many ways except the right one. Even consulted wordpress codex, but i always need more explicit examples. This worked, Thank you. – Paul C. Oct 8 '17 at 22:11

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.