• Hello, World!

    I’m curious to know if I can run multiple queries in one action? For example, I’d like to delete two meta keys at the same time from the wp_postmeta table using a query like this:

    DELETE FROM wp_postmeta WHERE meta_key = ‘metakey1, metakey2, etc…’;

Viewing 4 replies - 1 through 4 (of 4 total)
  • I believe the proper syntax is:

    . . . WHERE meta_key IN ('metakey1', 'metakey2', etc.)

    However, I think it’s a bad idea to bypass the provided WordPress API methods (e.g. delete_post_meta, etc.) and manipulate database table directly through SQL. The WordPress function might handle details, such as current post vs. previous revisions of the post, that you may have not necessarily considered, for example.

    Also, if a future version of WordPress changes how meta-keys are stored in the database, the related WordPress API functions will be updated accordingly– but your bypass code would not be. So you save yourself future maintenance headaches by sticking to the WordPress function.

    Thread Starter joinfof

    (@joinfof)

    I know my way around my database pretty well, is there any way you can give me specific instructions or possibly a video explaining the most efficient way to clean up my database? I’m reading over the article you suggested and I think it’s a little over my head.

    I’ve spent the past few months working on becoming more comfortable with sql queries, dropping columns, tables, localhost to a live server and so forth.

    Over the 1.5 years I’ve been “developing,” my trial & error has cluttered the db with uninstalled plugin and theme settings, data, etc.

    Keep in mind, my weak spot is PHP. I’m currently putting myself through CodeAcademy! Any help is appreciated!

    If you’re looking to clean old old and un-mathced entries from the databsae, do a search in the plugins area on here for databse cleaners. There’s a few plugins that do that, so it’s worth investigating that if you don’t want to get deep down into the SQL/PHP side of things just yet.

    Okay, I misunderstood your original intent . . . so the WordPress API suggestion makes less sense, obviously.

    The way I’d go about what you’re trying to do would be something like using phpMyAdmin (or a similar tool) to manually operate on the database. First thing I’d do before changing anything would be a full backup/export of the database, just in case. Hopefully, you won’t need it, but if you do, it’s there.

    Then I’d build my SQL queries with a SELECT first, DELETE second approach. Like this:

    1. SELECT * WHERE meta_key IN (‘metakey1’, ‘metakey2’, etc.)

    2. Count and inspect the records to make sure I’m getting what I wanted to get.

    3. DELETE * WHERE meta_key IN (‘metakey1’, ‘metakey2’, etc.)

    That way, if your WHERE clause turns out to be faulty somehow, you have a chance to catch it during the inspection phase of step 2 before you delete anything.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Multiple SQL queries’ is closed to new replies.