6 Responses

  1. Konstantin Kovshenin

    The only trouble with direct SQL queries to WordPress tables (whether via $wpdb or not) is that you’re by-passing everything: actions and filters defined by core, plugins and themes. For example, if you delete all comments to a post via direct SQL, the comment count in wp_posts is not updated for you. If you delete a post via SQL, WordPress won’t bust your page cache, XML sitemap cache, etc. If you change your post status from anything to publish, your Jetpack won’t send e-mails to your users about your new posts, and won’t cross-post that article to Twitter, etc.

    Worse is when you have persistent object caching turned on, because it will store the entire wp_options table to memcached or wherever, so any updates to the options table via SQL will not be reflected. Same for posts, pages and other data stored in object cache.

    Bottom line: it’s okay to do SQL, but you should be extremely careful and always test your queries locally, before running them in a production environment.

  2. Tom McFarlin

    First off, props on putting yourself out there and showing some of the things you’re learning (regardless of level of experience).

    At the risk of self-promotion (which I promise I’m trying to more relevant than promotional ;), this kind of hits on some of the comments I had in this post where developers where talking about being hesitant to share their work.

    That said, Konstantin is right. Editing the tables using a SQL frontend or even inline queries may not yield the result that you’d expect – that’s why I’m a big fan of using the API’s.

    Case in point: This week, I had someone want to change meta values for all all of their posts. Sure, writing a query woud’ve been easy, but I instead wrote a WP_Query, looped through the results, and saved the data that way.

    So, I guess, my question is are you doing this as an exercise in learning SQL or as an alternative way of updating database records to circumvent the API?

  3. Eric Hoanshelt

    Good job on the write up! Love seeing people getting into SQL with WordPress. It’s really a lost art! I deal with this stuff all the time when I’m going migration for http://erichoanshelt.me and http://thewpvalet.com.

    When dealing with migrating from a custom database schema to a WordPress schema, SQL is a must and I’m glad to see you’re jumping in! Keep up the good work!

  4. malike

    Take a look on a free tool — Valentina Studio. Amazing product! IMO this is the best manager for mysql for all platforms. http://www.valentina-db.com/en/valentina-studio-overview

Leave a Reply