I have tried dbDelta and $wpdb->query, they does not work. Does anyone has any ideas?

  • What is the error? You can dump it with the following call right after you executed the statement: $e = $wpdb->query($sql); die(var_dump($e)); – Thomas Apr 26 '12 at 8:47

As try today, truncate is WAY BETTER that DELETE, because DELETE broke the site, and trucate just empty the table, here is the working code :

global $wpdb;

$wpdb->query('TRUNCATE TABLE wp_posts');

According to this forum entry, truncate does not work. I would guess that since it's such a destructive query, that it's filtered by the class. There is also this Trac Post, which if I am reading it right, disallows DDL statements.

  • Well, maybe I use DELETE instead of truncate. – goodseller Apr 26 '12 at 14:51
  • @goodseller Not sure what you're trying to do, so it might not be important to you--but note that DELETE does not reset any auto-incrementing columns. So even though your table will be empty, the next item ID will pick up where the last auto- incremented value left off. – MathSmath Apr 26 '12 at 19:53
  • @MathSmath Yeah~ That's true, so true. However is there any way to do so? – goodseller Apr 27 '12 at 4:02
  • I found it: $wpdb->query("ALTER TABLE tableName AUTO_INCREMENT=0") Thanks @MathSmath :) – goodseller Apr 27 '12 at 5:37
  • 1
    1. linked forum entry code did not work because of a typo. 2. trac ticket just talks about return values from ddl statements. – skndstry Sep 13 '16 at 9:03

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.