47
votes
Accepted
How to change user password with wp-cli?
This does the trick:
wp user update USERNAME --user_pass="PASSWORD"
(Found it here.)
41
votes
WP CLI "Error establishing a database connection" in localhost (MAMP)
Go into your wp-config.php and change your DB_HOST to 127.0.0.1 instead of localhost.
Credit goes to Craig Wayne above in the comments.
35
votes
Accepted
Detect if WP is running under WP-CLI
Within the php/wp-cli.php we find these lines:
// Can be used by plugins/themes to check if WP-CLI is running or not
define( 'WP_CLI', true );
define( 'WP_CLI_VERSION', trim( file_get_contents( ...
34
votes
Detect if WP is running under WP-CLI
The canonical check for WP-CLI, which is used in most plugins and is explicitly mentioned in the docs, is the check whether WP_CLI is defined and set to true:
if ( defined( 'WP_CLI' ) && ...
24
votes
how to update serialized options programatically?
WP-CLI is definitely the answer to this after the update to 1.4.0 which introduced the pluck and patch commands for accessing serialized data in WordPress.
The pluck command takes this format for ...
21
votes
Accepted
How can I bulk delete media and attachments using WP-CLI?
From the WP-CLI documentation about wp post delete:
wp post delete --force $(wp post list --post_type='attachment' --format=ids)
See wp post list for additional information.
21
votes
Accepted
Why is it important to deactivate a plugin before deleting it?
Generally, plugins have some functionality hooked onto the deactivation action. This could be clearing cache, resetting options, you name it.
Therefore the best practice is to deactivate them first, ...
19
votes
How to change user password with wp-cli?
first check the user name:
wp user list
then change password without leaving traces in history
wp user update admin --prompt=user_pass
19
votes
How do I clone or duplicate a post with the WordPress Command Line Interface WP-CLI?
This can now be done via WP-CLI using $ wp post create --from-post=1. It even dupes the meta data.
19
votes
How I prevent a plugin to be loaded when doing WP-CLI?
You can use the skip-plugins option in WP-CLI to not load individual plugins when using WP-CLI.
You can either use it in command like this:
wp user list --skip-plugins=my-plugin
Or you can add this ...
17
votes
how to update serialized options programatically?
Solved it myself, this is how to do it:
If you want to programatically update serialized options:
download wp-cli from wp-cli.org
Find out what is the "key" for the options you are using. In this ...
16
votes
Running WP Cron on multisite the right way
I think the best way is to use WP-CLI but you'd need to write a bash script to do this. Here is one that should do it for you:
WP_PATH="/path/to/wp"
for SITE_URL in = $(wp site list --fields=domain,...
15
votes
Accepted
How to change user_login with wp-cli?
Not allowed by design:
If we try to change the user login by the email:
wp user update mary@example.tld --user_login=mary_new
or by the user id:
wp user update 123 --user_login=mary_new
we get ...
15
votes
Running WP Cron on multisite the right way
After you've added the constant in wp-config.php
defined('DISABLE_WP_CRON') or define('DISABLE_WP_CRON', true);
WP-CLI
And assuming you have your config.yml setup correctly, you can ommit the --...
14
votes
Accepted
How I prevent a plugin to be loaded when doing WP-CLI?
One of the first things WordPress does to load plugins is get the active plugins as saved in the database:
$active_plugins = (array) get_option( 'active_plugins', array() );
Since it uses get_option(...
13
votes
Accepted
WP-CLI Bulk delete posts from specific category
This should delete all posts in your category:
wp post delete $(wp post list --cat=your_category_ID --format=ids)
Or directly:
wp db query [<your_sql_query>]
For more info:
wp post delete --...
11
votes
Accepted
How can I batch delete all unattached images with WP-CLI or other automated process?
You can try this (untested) modification to the answer you linked to by @something
wp post delete $(wp post list --post_type='attachment' --format=ids --post_parent=0)
to delete attachments without ...
11
votes
Accepted
How to update an 'array' option using wp-cli
Thanks to a tip from milo in the comments above, I looked at this similar question.
There is an answer provided by Laurent which basically gets the option using wp-cli, pipes it to an inline php ...
11
votes
WP CLI "Error establishing a database connection" in localhost (MAMP)
For me, the answer was changing the setting for DB_HOST to 127.0.0.1:8889 instead of localhost in wp-config.php, and also checking the box to "Allow network access to MySQL" in the MySQL settings. ...
10
votes
WP-CLI - Selecting PHP version
Got the same problem! Just switch the php version.
On my server PHP5.6 was default for apache, while CLI was configured with PHP7.1. After installing WP-CLI, with wp --info I got this result:
PHP ...
10
votes
Accepted
Run WP-CLI using PHP
Regarding the wp --info output, that makes sense. If you don't have any packages installed (see wp package --help or a global configuration files (wp-cli.yml) then those items would be blank.
You ...
10
votes
WP-CLI How to generate a list of posts with corresponding meta values
Just figured it out. This isn't clearly or explicitly documented at https://developer.wordpress.org/cli/commands/post/list/ or elsewhere.
wp post list --fields=ID,post_title,Amazon.com,Amazon.co.uk ...
9
votes
wp-cli Enabling Maintainance Mode
You can enable maintenance mode in WordPress by adding a .maintenance file to your root WordPress directory. It will need to include:
<?php
$upgrading = time();
With this file in place, your site ...
9
votes
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', '...
9
votes
Accepted
How do I run a PHP script from Wordpress environment, like `wp shell`?
Yes, there is! I didn't find it at first, but wp cli offers a command called eval-file which does what you want:
$ echo '<?php echo is_wp_error("foo") ? 1 : 0;' > example.php
$ wp eval-file ...
8
votes
Using wp-cli can I not query pages by their title?
If we restrict us to the output of wp post list, then here's a way to search for %test% within the post titles of published posts:
wp post list --ignore_sticky_posts=1 --post__in=$(wp db query '...
7
votes
how to update serialized options programatically?
Here is how I do it in a bash script:
wp option get wp_smtp_options --format=json | php -r '
$var = json_decode( fgets(STDIN) );
$var->from = "email@email.com";
$var->fromname = "me";
print ...
7
votes
How to use wp-cli.yml file?
TL;DR:
Run wp version --debug
Check that wp-cli.yml was found
Check that the ABSPATH set by WP-CLI matches the directory where WP is installed
Look at find_wp_root() for hints about why it's finding ...
7
votes
Accepted
wp-cli Enabling Maintainance Mode
WP-CLI now has native commands for it.
# Activate Maintenance mode
$ wp maintenance-mode activate
# Deactivate Maintenance mode
$ wp maintenance-mode deactivate
See wp-cli/maintenance-mode-command ...
7
votes
Fastest way (least amount of steps) to locally import a remote database using WP-CLI
Since WP-CLI 0.24.0 you can now use aliases which enable you to import a remote database quite easily.
By using aliases, you can run WP-CLI commands against another WP-CLI install. That install could ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
wp-cli × 352multisite × 22
command-line × 19
plugins × 16
php × 15
database × 15
installation × 13
updates × 10
plugin-development × 9
unit-tests × 9
migration × 8
posts × 7
customization × 7
post-thumbnails × 7
mysql × 7
options × 7
linux × 7
woocommerce-offtopic × 6
images × 6
themes × 6
users × 6
windows × 6
media × 5
export × 5
automation × 5