WPCLI
quick cheat sheet

OverviewWPCLI CronWPCLI DatabaseTab Four

https://developer.wordpress.org/cli/commands/

Below is a listing of all currently available WP-CLI commands with links to documentation on usage and subcommands.

Looking to learn more about the internal API of WP-CLI or to contribute to its development? Check out the WP-CLI team’s handbook.

Command Description
wp admin Open /wp-admin/ in a browser.
wp cache Adds, removes, fetches, and flushes the WP Object Cache object.
wp cap Adds, removes, and lists capabilities of a user role.
wp cli Reviews current WP-CLI info, checks for updates, or views defined aliases.
wp comment Creates, updates, deletes, and moderates comments.
wp config Generates and reads the wp-config.php file.
wp core Downloads, installs, updates, and manages a WordPress installation.
wp cron Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
wp db Performs basic database operations using credentials stored in wp-config.php.
wp dist-archive Create a distribution archive based on a project’s .distignore file.
wp embed Inspects oEmbed providers, clears embed cache, and more.
wp eval Executes arbitrary PHP code.
wp eval-file Loads and executes a PHP file.
wp export Exports WordPress content to a WXR file.
wp find Find WordPress installations on the filesystem.
wp help Gets help on WP-CLI, or on a specific command.
wp i18n Provides internationalization tools for WordPress projects.
wp import Imports content from a given WXR file.
wp language Installs, activates, and manages language packs.
wp maintenance-mode Activates, deactivates or checks the status of the maintenance mode of a site.
wp media Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
wp menu Lists, creates, assigns, and deletes the active theme’s navigation menus.
wp network Perform network-wide operations.
wp option Retrieves and sets site options, including plugin and WordPress settings.
wp package Lists, installs, and removes WP-CLI packages.
wp plugin Manages plugins, including installs, activations, and updates.
wp post Manages posts, content, and meta.
wp post-type Retrieves details on the site’s registered post types.
wp profile  
wp rewrite Lists or flushes the site’s rewrite rules, updates the permalink structure.
wp role Manages user roles, including creating new roles and resetting to defaults.
wp scaffold Generates code for post types, taxonomies, plugins, child themes, etc.
wp search-replace Searches/replaces strings in the database.
wp server Launches PHP’s built-in web server for a specific WordPress installation.
wp shell Opens an interactive PHP console for running and testing PHP code.
wp sidebar Lists registered sidebars.
wp site Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
wp super-admin Lists, adds, or removes super admin users on a multisite installation.
wp taxonomy Retrieves information about registered taxonomies.
wp term Manages taxonomy terms and term meta, with create, delete, and list commands.
wp theme Manages themes, including installs, activations, and updates.
wp transient Adds, gets, and deletes entries in the WordPress Transient Cache.
wp user Manages users, along with their roles, capabilities, and meta.
wp widget Manages widgets, including adding and moving them within sidebars.

Regerate – thumbs – in sequence

seq 129000 129421 | xargs wp media regenerate

Wpcli Regenthumbs Sequence
Wpcli Regenthumbs Sequence


WP-CLI: Backup

https://www.rochen.com/2017/08/29/backup-and-restore-wordpress-files-folders-and-content-with-wp-cli/

Before updating anything, it is important to first backup your website, including your database and files. WP-CLI makes the hardest part of this process easy.

WordPress Database Backup

At the prompt, type this and press Enter:

wp db export

Now you have a backup of your database in an .SQL file at the root of your website.

Zip Database

[code]tar -vczf ~/public_html/clickety-clack-click/shawnef8_fr0gcl3ck-2021-02-05-c5fcb9f.tar ~/public_html/clickety-clack-click/shawnef8_fr0gcl3ck-2021-02-05-c5fcb9f.sql[/code]

  • c: Create my backup archive.
  • z: Compress the archive with gzip.
  • v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
  • f: Allows you to specify the filename of the archive.
    ~/public_html/sitename/mybackupfilename.tar: Store my WordPress site backuped files and folders inside the “mybackups” directory and call the backup file mybackupfilename.tar.

  • /public_html/site/: path to the WordPress files and folders to be backed-up.
WordPress File System Backup

Copying files from the server is not available in WP-CLI, but you can quickly compress all of your files, including the recent .SQL backup of your database.

Make sure you are in the root of your website, as we described earlier. Using the tar command with a few special parameters that make the backup better, and the trailing period at the end is very important, it ensures the new compressed file is saved in the same directory (i.e., folder).

Step #3. Backup your WordPress files and folders.

For security purposes, it is recommended not to store sensitive data like backups within a web accessible directory. By default on Rochen’s hosting platform this is the “public_html” or “www” directory. To store your future backups securely outside of the web accessible public_html folder, we recommend creating a directory, for example, “mybackups”, outside it.

  • To create such a directory, in your SSH client run the following command:

[code]mkdir mybackups[/code]

If you wish to verify successful creation of the new directory outside of “public_html” you should now see the new directory at the same level as “public_html” within the account root using cPanel’s file manager or any SFTP client:

  • To backup your WordPress site’s files and folders and store them in this new directory, use the backup ‘tar’ command:

[code]tar -vczf ../mybackups/mybackupfilename.tar ~/public_html/sitename/mybackupfilename.sql[/code]

Let’s take a look at this command in more detail.

-c: Create my backup archive.
-z: Compress the archive with gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
../mybackups/mybackupfilename.tar: Store my WordPress site backuped files and folders inside the “mybackups” directory and call the backup file mybackupfilename.tar.
~/public_html/sitename/: path to the WordPress files and folders to be backuped.

If you now take a look inside of your “mybackups” directory, you will see this backup archive file: mybackupfilename.tar:

How to backup and restore WordPress content with WP-CLI’s ‘export’ command

If you are familiar with the “Export” feature in the web-based WordPress administrator dashboard to backup WordPress pages, posts, comments, custom fields, and tags, then WP-CLI allows you to do the same over the SSH command line.

WP-CLI often allows for such tasks to be completed quicker and in the case of larger sites with less server load on your hosting account than with the web-based alternative. You can use WP-CLI commands only from inside the folder, where you installed your WordPress site. In this example case, this is the “public_html” directory. Let’s move to that directory.

  • Run the following command:

[code]cd public_html[/code]

[code]mkdir backupfoldername[/code]

  • Once this command executes (1), you will now see the “public_html” directory name appeared to the left of the $ sign (2).
  • To create a backup of your WordPress site content using WP-CLI, and securely store it in the “mybackups” folder, simply type the following command in the SSH client window:

[code]wp export –dir=[/code]

Generates one or more WXR files containing authors, terms, posts, comments, and attachments. WXR files do not include site configuration (options) or the attachment files themselves.

OPTIONS

[–dir=]
Full path to directory where WXR export files should be stored. Defaults
to current working directory.
Generates one or more WXR files containing authors, terms, posts, comments, and attachments. WXR files do not include site configuration (options) or the attachment files themselves.

OPTIONS

[–dir=]
Full path to directory where WXR export files should be stored. Defaults
to current working directory.

  • Once this command (1) has finished running, in the SSH command line prompt you’ll see this message “Success: All done with export.” (2):
  • [code]tar -vczf ../backupfoldername.tar ~/public_html/backupfoldername[/code]

WP-CLI will also create a .xml file. The filename will be similar to the example in the image below: learningwp-cli.wordpress.2017-08-20.000.xml:

WP-CLI ‘export’ command options and filters

To allow you fine tune your WordPress content backup, the WP-CLI ‘export’ command offers some options and filters.

You can choose to export only content written explicitly by a certain writer, or created during a certain period of time. You could also choose to export only content with specific ID numbers, or content without comments. To do this, use the ‘export’ command options and filters. Let’s have a look at some examples.

Examples of running WP-CLI ‘export’ command with options and filters

Example #1. Export posts, published by the user ‘joedoe’:

[code]wp export –dir=../mybackups –user=joedoe[/code]

Example #2. Export posts, published by the user ‘joedoe’ from 1 January 2017 to 1 March 2017:

[code]wp export –dir=../mybackups –user=joedoe –start_date=2017-01-01 –end_date=2017-01-03[/code]

(please note that the start and end date filter format is YYYY-DD-MM)

Example #3. Export posts (and skip their comments), published by the user ‘joedoe’ from 1 January 2017 to 1 March 2017:

[code]wp export –dir=../mybackups –skip_comments –user=joedoe –start_date=2017-01-01 –end_date=2017-01-03[/code]

Example #4. Export all posts with status “Pending”

[code]wp export –dir=../mybackups –post_status=pending[/code]

Example #5. Export all posts, pending in the category “News”

[code]wp export –dir=../mybackups –post_status=pending –category=news[/code]

How to restore WordPress content

To be able to restore WordPress content from a previously created content backup .xml file, you first need to install the “WordPress Importer” plugin. To do this, just run the following WP-CLI command within your SSH client:

[code]>wp plugin install wordpress-importer –activate[/code]

Now you are ready to proceed to import:

[code]wp import ../mybackups/learningwp-cli.wordpress.2017-08-20.000.xml –authors=create[/code]

Once your ‘import’ command finishes, in your SSH command line prompt you should see the success message:

Transfer Backups

This will only get your backup file compressed and ready. From there, like with any good backup, you should transfer it off the server. You can use FileZilla with SFTP, your SSH credentials should work.

Using

wp export [--dir=<dirname>] [--stdout] [--skip_comments] [--max_file_size=<MB>] [--start_date=<date>] [--end_date=<date>] [--post_type=<post-type>] [--post_type__not_in=<post-type>] [--post__in=<pid>] [--with_attachments] [--start_id=<pid>] [--max_num_posts=<num>] [--author=<author>] [--category=<name>] [--post_status=<status>] [--filename_format=<format>]

Generates one or more WXR files containing authors, terms, posts,
comments, and attachments. WXR files do not include site configuration
(options) or the attachment files themselves.

OPTIONS

[--dir=<dirname>]
	Full path to directory where WXR export files should be stored. Defaults
	to current working directory.

[--stdout]
	Output the whole XML using standard output (incompatible with --dir=)

[--skip_comments]
	Don't include comments in the WXR export file.

[--max_file_size=<MB>]
	A single export file should have this many megabytes. -1 for unlimited.
	---
	default: 15
	---

FILTERS

[--start_date=<date>]
	Export only posts published after this date, in format YYYY-MM-DD.

[--end_date=<date>]
	Export only posts published before this date, in format YYYY-MM-DD.

[--post_type=<post-type>]
	Export only posts with this post_type. Separate multiple post types with a
	comma.
	---
	default: any
	---

[--post_type__not_in=<post-type>]
	Export all post types except those identified. Separate multiple post types
	with a comma. Defaults to none.

[--post__in=<pid>]
	Export all posts specified as a comma- or space-separated list of IDs.
	Post's attachments won't be exported unless --with_attachments is specified.

[--with_attachments]
	Force including attachments in case --post__in has been specified.

[--start_id=<pid>]
	Export only posts with IDs greater than or equal to this post ID.

[--max_num_posts=<num>]
	Export no more than <num> posts (excluding attachments).

[--author=<author>]
	Export only posts by this author. Can be either user login or user ID.

[--category=<name>]
	Export only posts in this category.

[--post_status=<status>]
	Export only posts with this status.

[--filename_format=<format>]
	Use a custom format for export filenames. Defaults to '{site}.wordpress.{date}.{n}.xml'.

EXAMPLES

# Export posts published by the user between given start and end date
$ wp export --dir=/tmp/ --user=admin --post_type=post --start_date=2011-01-01 --end_date=2011-12-31
Starting export process...
Writing to file /tmp/staging.wordpress.2016-05-24.000.xml
Success: All done with export.

# Export posts by IDs
$ wp export --dir=/tmp/ --post__in=123,124,125
Starting export process...
Writing to file /tmp/staging.wordpress.2016-05-24.000.xml
Success: All done with export.

# Export a random subset of content
$ wp export --post__in="$(wp post list --post_type=post --orderby=rand --posts_per_page=8 --format=ids)"
Starting export process...
Writing to file /var/www/example.com/public_html/staging.wordpress.2016-05-24.000.xml
Success: All done with export.

Installing

This package is included with WP-CLI itself, no additional installation necessary.

To install the latest version of this package over what’s included in WP-CLI, run:

wp package install git@github.com:wp-cli/export-command.git

Contributing

We appreciate you taking the initiative to contribute to this project.

Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

For a more thorough introduction, check out WP-CLI’s guide to contributing. This package follows those policy and guidelines.


10+ WP-CLI Tricks to Manage Your WordPress Site (Updated for WP-CLI 2.3.0)

https://www.codeinwp.com/blog/wp-cli/

Note. WP-CLI 2.3.0 has just been released, which makes it the perfect time to have a deeper look into what WP-CLI has to offer in general, as well as how to best start working with it. Let me warn you, though, if you haven’t experimented with WP-CLI yet, get ready to be impressed when you see what’s possible.

WordPress is a wonderful project, but as with any software you may find yourself wishing you had some kind of backdoor access to your code and database. Those of you who have ever tried resetting your admin credentials with a broken reset password link know what we’re talking about!

Thankfully, there is a solution that can help. The WP-CLI project gives you command-line access to your WordPress installation, which means you can manage most of your website using a few quick instructions within the terminal.

For example, if your “reset password” link is broken, you can simply change your password using the WP-CLI instead. There are many similar situations where WP-CLI comes in handy. Plus, we just love the hacker-like feeling that comes with working in the command line.

In this article, we’ll introduce some of the typically irritating tasks that WP-CLI makes almost a fun experience, and show you how to accomplish them using the tool. Let’s get cracking!

TL;DR on the WP-CLI project

WP-CLI stands for WordPress Command Line (homepage), since this tool gives you access to WordPress’ management system via the command line.

Typically, you have to use the online user interface to manually manage a WordPress website (the wp-admin). And with that, taking care of a broken site or managing transitional periods often involves digging through files using FTP or trying to write custom PHP code. WP-CLI simplifies the process of working on your site by giving you a direct-access shorthand, circumventing the need for extra plugins.

If you’ve never used the command line before, there is a small learning curve. However, once you get going, WP-CLI can significantly speed up the way you work with your website. You’ll be able to manage complex database, plugin and theme changes with a few keystrokes. If you like the plain-text interface, you can even use it to moderate comments, options, and menus. (All of which we’ll cover further in this guide.)

Long story short, WP-CLI makes many difficult tasks simple and fast. What’s more, WP-CLI is also extendable. If you have custom needs, it is possible to write unique scripts that run using WP-CLI to your specifications.

Before we move forward, it’s important to realize that WP-CLI is not a plugin for your WordPress site, but a tool that is installed on your server. In other words, if you want to use WP-CLI, you will need to install it both on your local development environment and your live hosting server for consistent access. This may mean finding a host that offers WP-CLI pre-installed.

(Note; out of our top recommended web hosting choices for WordPress, SiteGround, A2 Hosting, and WP Engine all have WP-CLI installed by default for their customers; in other words, you can begin using WP-CLI as soon as you’re done setting up your hosting account with those companies).

Some further reading on the WP-CLI basics:

How to manage your WordPress site using WP-CLI

Once WP-CLI is installed, you can use the command line to open the folder for the WordPress website you’d like to manage. If you’re working live on your hosting server, you’ll need secure shell access.

However, we recommend trying these commands out in a development environment first! There is no undo button, so you’ll want to feel fairly comfortable with this tool before using it on a live server. To get you started, here are four useful ways to practice using WP-CLI on your site.

1. Have a quick look into the wp-config.php file

WP-CLI has always been quite efficient when it comes to letting you configure a fresh site. For instance, the handy wp config create command lets you create a fresh config file and put all the important details in it. Usually much quicker than doing this the traditional way, especially if you’re a fast typer.

But WP-CLI takes it a step further. There are two subcommands for the main wp config command.

The first one is this:

wp config get
  • lists constants and globals as defined in your current wp-config.php file.

The other one is this:

wp config path
  • simply displays a path to the current wp-config.php file.

If you want to get even more hands-on with your WordPress install, you can also use another WP-CLI command:

wp db size --tables

Quite useful in helping you decide whether you need some database optimization or not.

2. Run core updates

Usually, running an update involves logging into your admin and clicking through a couple of pages to update your site.

With WP-CLI, updates are easily run with one simple command:

wp core update

What makes this approach unique is that it can accept arguments to customize what happens when you run it. Let’s say you recently updated to a WordPress version that broke a critical plugin on your site. The author of that plugin hasn’t had the chance to update yet, but you need your site to work properly. While it’s best to keep an updated site, you can change the core by adding the version flag to your command and rolling back to the point where there wasn’t a conflict:

wp core update --version=4.7.1

Another useful feature of a script-based solution like WP-CLI is that you can schedule it to run on a regular basis for all the sites you have installed. It is incredibly scalable for those who manage multiple sites. For example, using a WP-CLI add-on like WP Tools from Bluehost allows you to automatically run updates for every website on your server.

3. Update, disable, and delete pesky plugins

Usually, if a plugin breaks your site completely (or even just disables your admin), you have to log in via FTP and change its folder name in order to deactivate it. On the other hand, there is a simple WP-CLI command to deactivate plugins:

wp plugin deactivate plugin-name-example

If a plugin is giving you serious trouble and needs to be deleted, you can do so using the delete command instead:

wp plugin delete plugin-name-example

Also, you can skip manual updates for plugins by running a command similar to the WordPress core update feature:

wp plugin update --all

And, probably something I should have mentioned at the beginning, WP-CLI also allows you to check the status of the plugins currently on your site. For that, use:

wp plugin status

Individually, it might not seem like these commands would save you much time. However, it’s important to remember that directly accessing the server is much faster than waiting for a web browser to interpret all the scripts and styles associated with a graphical user interface. If you frequently need to manage these types of tasks, you may be surprised at how quickly your extra time adds up.

4. Reset user passwords

Imagine that you’ve lost your WordPress admin password. You’ve tried the reset password link, but for some reason the reset email won’t show up in your inbox. Are you locked out of your WordPress site forever? Not with WP-CLI.

WP-CLI offers speedy user management, including the ability to reset passwords using the following line:

wp user update adminuser@example.com --user_pass=new-password-example

Successful user credentials update with WP-CLI.

With commands as simple as this, there’s no need to panic or worry about complicated workarounds to get access to your site again. Gone are the days of manually hashing your password and inserting it directly into the database…

Moreover, since this is all done over a secure connection with your server, you’re also not sacrificing the security of your website by using this method.

5. Backup and manipulate the database

WordPress doesn’t make it easy to directly interact with your database. This can be frustrating when you want to accomplish seemingly simple tasks, like running an update without a plugin or searching for and replacing a set phrase.

You can use WP-CLI to make these jobs easier. It allows you to optimize and repair databases with simple commands like:


wp db optimize
wp db repair

If you are writing a custom query, you can quickly test it out by using the query command:

wp db query "SELECT * FROM wp_options"

However, before you get too trigger-happy with the database, it’s important that you know how to create backups. The command for creating SQL backups in WP-CLI is as follows:

wp db export

This will create an SQL file* you can use to restore the database of your site if something goes wrong. It even works well when used in combination with backup plugins.

* The name the file’s going to be given is rather random. To assign your own, use this:

wp db export yourname.sql

6. Clean up post revisions

Post revisions are something we have a bit hard time with here at CodeinWP. Some of our most frequently updated posts have 200+ revisions, which makes managing things really tough.

Mainly, when you go to the standard revision comparison page in WordPress (next screenshot), you get that bar at the top. Through it, you can choose the two revisions that you want to compare. This is fine if you have sub-20 revisions on a post. But with more than 100, the bar becomes un-clickable – I mean, you can’t easily point to the exact revision that you want – requires pixel perfection in your clicks. The bar is just too dense.

To battle this in some way, you can set a limit on the number of revisions allowed per post in your wp-config.php file (eg. define('WP_POST_REVISIONS', 40);). But this only limits the revisions on your future actions. Removing all the existing revisions that are above that mark is on you. This can be done in a couple of ways. One of them is via a plugin. The other is via WP-CLI.

And the WP-CLI way is just much quicker. To get started, you need to install an additional WP-CLI package called wp-revisions-cli.

This can be done via a simple command:

wp package install trepmal/wp-revisions-cli

After that, you get to use a handful of cool new WP-CLI commands:

wp revisions clean
  • removes old revisions on all posts. It defaults to the value of WP_POST_REVISIONS – keeping only the most recent revisions.

Alternatively, if you want to be more hands-on with your revisions, you can, for instance, control the revisions for a specific post:

wp revisions list --post_id=ID
  • gives you a list of all the revisions for a given post.

Or:

wp revisions clean --post_id=ID --before-date=DATE
  • removes revisions on a given post published on or before some date (YYYY-MM-DD).

Overall, a really cool feature, and particularly if you tend to update your existing posts quite often – which naturally produces a lot of revisions. I’m really excited to see what new features the wp-revisions-cli package will bring in the future.

7. Create a child theme, like, now(!)

Granted, creating child themes is not supper difficult. But you still have to first go to the official reference at Codex – just to make sure that some fine details haven’t been changed, then create a functions.php and CSS, put that in a new directory, and so on and so forth.

WP-CLI gives you a quicker method. All you do is this:

wp scaffold child-theme NEW-CHILD-SLUG --parent_theme=SLUG --theme_name=TITLE

… and that’s all. What you get as a result is an operational, blank child theme based on a set parent theme.

There are some additional parameters available, too. For instance, you can do this:

wp scaffold child-theme SLUG --parent_theme=SLUG --theme_name=TITLE --author=FULL-NAME --author_uri=URI --theme_uri=URI --activate --force
  • --force overwrites files that already exist.

8. Create loads of dummy content for testing

When working on any sort of a WordPress project, dummy content is something you probably need relatively often.

  • Maybe you’re working on some new plugin that processes a number of posts and then does something to them.
  • Maybe you want to see if your pagination is working just fine.
  • Maybe you want to put more stuff in your menus, for testing purposes.
  • And so on…

Normally, what I used to do in such cases was press Ctrl+F and start looking for that pesky XML dump of example WordPress content that I have sitting around somewhere. But this takes time, and I seem to be misplacing the file every time somehow.

Anyway, as you’d imagine, WP-CLI has some options for that too:

wp post generate --count=10
  • creates 10 empty posts; but you don’t need to stop at just 10 – WP-CLI promises that 1000 is also a perfectly executable value.

Or, to make things more factual:

curl http://loripsum.net/api/4 | wp post generate --post_content --count=10
  • creates 10 posts, each having 4 paragraphs of dummy lorem ipsum text imported from loripsum.net.

9. Blank your site

There’s one really cool WP-CLI command that lets you empty your site of all its contents (including posts, comments, terms, and meta), while leaving the site configuration and users intact. It’s this:

wp site empty

If you tend to use the same dev WordPress setup for most of your tests/experiments/etc. then you know right away how much time this thing will save you.

While you can achieve the same effect by going through your content manually, or using phpMyAdmin, this WP-CLI command is in a league of its own when it comes to speed.

You can also do:

wp site empty --uploads
  • gets rid of everything in your “uploads” folder as well.

10. Remove all spam comments

First of all, WP-CLI gives you a lot of possibilities in regards to managing comments. For instance, you can:

  • create new comments with wp comment create
  • update comments with wp comment update
  • approve comments with wp comment approve
  • and a lot more

But perhaps the most interesting thing you can do here is erase all the spam comments from your site at once. This is particularly helpful if you ever wake up to thousands of new spam comments in your database.

The command is this:

wp comment delete $(wp comment list --status=spam --format=ids)

This is a neat hack. The main wp comment delete command expects an argument – the ID(s) of the comment(s) that should be deleted. So to provide the ID(s), we’re calling another command – wp comment list – which displays a list of all the comments that have been marked as spam. Quite cool, isn’t it?

11. Tame wp-cron

The standard wp-cron mechanism hasn’t been the most reliable thing. More or less, when you schedule a post in WordPress, you can never be 100% sure that it will indeed go live (at least my experience).

WP-CLI can help you tame cron just a bit with the use of this command:

wp cron event run --due-now
  • triggers all cron events due right now.

Bonus: Two “meta” tricks for WP-CLI

As awesome as WP-CLI is, it’s still a text interface, which means it’s difficult to always remember how to use this or that command.

There are two things you can do to help yourself out:

First, the standard help command:

wp help COMMAND
  • displays help on any command in WP-CLI.

Next, there’s the --prompt argument that works with most (or even all?) WP-CLI commands. For example, if I attempt to generate some new content like this:

wp post generate --prompt

WP-CLI will list all the arguments that are available for the generate command, and let me fill in the blanks (I can press Enter on things that are optional – inside [] brackets). Like so:

 

Managing WordPress Cron Events with WP-CLI

https://support.pagely.com/hc/en-us/articles/360031050071-Managing-WordPress-Cron-Events-with-WP-CLI

If you need to make changes to scheduled events within your WordPress site, WP-CLI is a great tool to have. In this article, we'll show you how to run, create, and delete events within the WordPress cron, using WP-CLI.

DISPLAYING ALL CRON EVENTS

To display a list of all cron events currently scheduled on your WordPress site, use the following command from within your site's root directory:

wp cron event list

RUNNING ALL WORDPRESS CRON EVENTS

If you want to trigger all currently due cron events (events that are due but haven't been run yet), you can run them with the following command:

wp cron event run --due-now

MANUALLY RUNNING A SCHEDULED CRON EVENT

To run all WordPress scheduled events manually, regardless of if they are due or not, you can use the following WP-CLI command:

wp cron event run --all

SCHEDULING A NEW CRON EVENT

Additional WP-Cron events can also be scheduled using WP-CLI. New cron events can be scheduled for any action hook within your site.

Scheduling a New Event for the Next Cron Run

The following command will schedule a new cron event for the desired action hook. This event will be triggered on the next time that the cron is run.

Be sure to replace my_example_action with the name of the action hook that you want to execute.

wp cron event schedule my_example_action

Scheduling a New Event for a Specific Date/Time

Scheduling a WordPress cron event to run on a specific date and time is similar to the example in the previous section, but requires an additional parameter that includes the date and time that the event should run.

wp cron event schedule my_example_action '+1 hour'

As you can see from the example above, a new event will be scheduled to run my_example_action in 1 hour.

Since the time parameter allows for any string that can use converted to a timestamp with the PHP strtotime() function, the following examples would also be valid:

Example: Run the Event on a Specific Date

wp cron event schedule my_example_action '7 July 2019'

Example: Run the Event in 1 Week

wp cron event schedule my_example_action '+1 week'

Example: Run the Event Next Monday

wp cron event schedule my_example_action 'next Monday'

For more information on the available formats, see the official PHP documentation on supported date and time formats.

Scheduling a New Recurring Cron Event

To create a new WordPress cron event that will repeat after a designated interval, you'll simply pass another argument containing how often the event will be run:

wp cron event schedule my_example_action now daily

In this example, we're using a WP-CLI command to tell WordPress to run my_example_action now and to run again once per day.

The available intervals are based on any currently defined cron intervals. By default, WordPress contains the following interval names:

  • hourly: Runs the event once per hour.
  • twicedaily: Runs the event twice per day (every 12 hours).
  • daily: Runs the event once per day (every 24 hours).

If you need additional cron intervals, you can add them using the cron_schedules filter within your plugin or theme.

DELETING A SCHEDULED CRON EVENT

Deleting a scheduled cron event with WP-CLI is quite straightforward. To delete an existing event, run the following command, replacing my_example_action with the name of your event action hook:

wp cron event delete my_example_action

https://support.pagely.com/hc/en-us/articles/203339430-WP-CLI-Database-Commands

WP-CLI Database Commands

WP-CLI has some powerful tools built in for database management, here are some of the most used/convenient features. (If you haven't done so please view Using WP-CLI Command Line Tools first)

Note: Please use caution when performing commands on the database and always make sure to take a backup first.

BACKING UP YOUR DATABASE

Exports a .sql dump into your site directory.

For more information on creating database backups with WP-CLI, take a look at our tutorial on exporting databases with WP-CLI.

Example

wp db export backup_name.sql

IMPORTING A DATABASE

Imports the database .sql file that you indicate.

For more information, take a look at our tutorial on importing databases with WP-CLI.

Example

wp db import new_database.sql

CHECKING YOUR DATABASE FOR ERRORS

Checks your connection settings and runs the MySQL CHECK TABLE statement.

Example

wp db check

RESETTING YOUR WORDPRESS DATABASE

This will reset the database to a fresh wp_ instance.

Example

wp db reset

OPENING A MYSQL CLIENT TO PERFORM MYSQL QUERIES

Connects to the database and opens the mysql command line client

Example

wp db query

EXPORTING AN .XML FILE

Exports the database in .xml/wxr format.

Note: Please see the documentation for handling authors from the wp-cli.org site

Example

wp export

IMPORTING AN .XML FILE

Imports the .xml file you indicate.

Example

wp import filename.xml

SEARCH AND REPLACE

Performs a search and replace function on the database that also handles serialized arrays.

The –all-tables flag will update tables that don't have the same table prefix that is used by WordPress.

You can also use –precise which uses PHP and is a bit more thorough but it takes longer so it should be used after the first process did not correct everything. Please consider cache on the server and in your local browser before using that though.

Note: Multi-site should use this article

Example

wp search-replace 'http://old-domain.com' 'http://new-domain.com' --all-tables


Tab Four

Scroll to Top