Hot answers tagged

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.)
user avatar
  • 1,277
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.
user avatar
  • 519
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( ...
user avatar
  • 64.4k
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' ) && ...
user avatar
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 ...
user avatar
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.
user avatar
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, ...
user avatar
  • 326
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
user avatar
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.
user avatar
  • 1,321
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 ...
user avatar
  • 3,654
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 ...
user avatar
  • 501
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,...
user avatar
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 ...
user avatar
  • 64.4k
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 --...
user avatar
  • 9,479
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(...
user avatar
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 --...
user avatar
  • 1,895
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 ...
user avatar
  • 64.4k
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 ...
user avatar
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. ...
user avatar
  • 211
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 ...
user avatar
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 ...
user avatar
  • 601
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 ...
user avatar
  • 241
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 ...
user avatar
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', '...
user avatar
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 ...
user avatar
  • 595
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 '...
user avatar
  • 64.4k
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 ...
user avatar
  • 212
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 ...
user avatar
  • 4,438
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 ...
user avatar
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 ...
user avatar
  • 648

Only top scored, non community-wiki answers of a minimum length are eligible