This is not the first time I’ve mentioned this, but one of the challenges that come with talking about writing direct database queries to update information in the WordPress database is that you leave yourself open to something like:
Yeah, but there’s an API to do something just like that.
And, in many cases, that’s right. I’m a big fan of using ’em, too. But there are times where direct, parameterized database queries can be a more optimal choice.
This is, of course, contingent on the environment in which you’re working the requirements of your project.
So this is yet another post that’s going to be an illustration of how to use $wpdb to quickly update information based on metadata (without using something like WP_Query or WP_Meta_Query to handle it).
As PHP has continued to move forward and WordPress (and its developer-economy) works to begin using more of the more modern features, one of the things we still see happening is the use of globals within WordPress.
To some degree, it’s inevitable: Significant parts of the application were built using globals when that was the facility that was a primary feature; other parts, though, have moved forward by providing classes or functions that prevent us from needing to do that.
Case in point: get_post_field is a solid alternative to using the $post global that provides much of the same information in a slightly more modern (and arguably safer) way of reading the data.
Not only does the language natively support functions for doing this, newer features of ES6 give us more powerful ways to build client-side scripts. And if you’re using jQuery with WordPress, then you have the same library of functions for, ahem, querying the DOM that we’ve had for years.
Other than, of course, we’re doing so on the server.
Before wrapping up our discussion on Composer, we have one important thing left to discuss: The vendor directory (and by extension, the Composer lock file).
Specifically, we need to talk about why we don’t need to commit the vendor directory to the repository but how our contributors can be sure they have the latest version of the software needed to work with our code base.
Using code quality tools to write better WordPress code is important, yes, but understanding how to properly manage dependencies and our repository is important, too. So before looking at said utilities, let’s review the lock file, the role it plays, and why we don’t need to commit the vendor directory to our repository.