WPCLI search and replace variants for all tables

In a WordPress installation, a certain number of database tables are registered in the $wpdb object. To see what they are, you can run the WP-CLI command wp db tables. On my local WP installation, these are the $wpdb-registered tables: wp_users wp_usermeta wp_posts wp_comments wp_links wp_options wp_postmeta wp_terms wp_term_taxonomy wp_term_relationships wp_termmeta wp_commentmeta wp_blogs wp_signups wp_site … Read more

WordPress API – count posts

Assuming you are using Linux or OS X, the easiest way is probably to use wp-cli (if present in your WordPress installation) to return a list of all posts: wp-cli post list Then pipe it to the word count tool to get the number of lines: wc -l Finally, deduct one to take care of … Read more

WP-CLI with theme: Uncaught Error: Class ‘WP_CLI’ not found

You’ll need to wrap your statement within a class_exists() check: function foo_command( $args ) { WP_CLI::success( $args[0] ); } if ( class_exists( ‘WP_CLI’ ) ) { WP_CLI::add_command( ‘foo’, ‘foo_command’ ); } This way, the command is only registered when the WP_CLI class exists — because the WP_CLI class will only exist when WP-CLI is running.

Is it possible to use WP-CLI via PHP-framework?

The WP_CLI class has a runcommand method that launches a new child process to run a specified WP-CLI command. According to the docs, you can use it like this: $options = array( ‘return’ => true, // Return ‘STDOUT’; use ‘all’ for full object. ‘parse’ => ‘json’, // Parse captured STDOUT to JSON array. ‘launch’ => … Read more

Trying to install WordPress on WSL Windows 10, and running into issues with wp-cli and “wp db create”

The error has nothing to do with your setup. It says that your connection credential to MySQL database is incorrect. ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES) When you were setting up the MySQL by following the tutorial sudo apt install -y lamp-server^ It probably did ask you to set a … Read more

What’s a better workflow for database migration?

I use this for search and replace on databases – https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ Simply move files with FTP, export and import database and then run the script searching for old URL and replacing with the new URL. Then I resave permalinks and I’m done. I’ve moved WP thousands of times using this. I’ve got the old script … Read more

How do I update a nested option?

wp option does all you need. See wp option patch in particular. Updates a nested value in an option. Get a nested option value: $ wp option pluck ez-toc-settings auto_insert_post_types Set a nested option value (with an array): $ wp option patch update ez-toc-settings auto_insert_post_types ‘{“post”:”post”}’ –format=json