WP-CLI logoWP-CLI is the command-line interface for WordPress. It is a PHP package that you have to install on your web server.

Once installed, you can use the wp command in the terminal to update plugins, configure multisite installations and much more, without using a web browser.

FacetWP WP-CLI support

FacetWP includes built-in support for using WP-CLI to do a full or partial re-index, or to purge FacetWP’s index table. See the available commands below.

You can use WP-CLI to manually trigger re-indexing, or you can use a WP-CLI command in a cron job to re-index at pre-defined intervals.

Re-indexing with WP-CLI is the recommended approach for indexing high-content sites, or sites with large amounts of content imported regularly. In these cases the automatic indexing process can become problematic. Using WP-CLI will give you full control over when the indexing process runs exactly, and at which frequency. In this approach, FacetWP’s automatic indexing can be turned off with the facetwp_indexer_is_enabled hook.

How to trigger indexing with WP-CLI

Once WP-CLI is installed, follow these steps to trigger a full re-index:

Log in with SSH

Open a new terminal window on your computer and SSH into your server with your user account.

You can also log in as root user, but then WP-ClI will display a security warning when you enter a command. If you logged in as root, you can switch to your user account with:

su yourusername

First make sure WP-CLI is installed correctly, in which case the following command will output information about the installation, like the version and directory it is installed in:

wp --info

To see in which directory you are in your user account, you can use the ls command, which will display a listing of all files and directories at the current level.

ls

To be able to use the indexing commands, you need to navigate to the directory your WordPress installation is in (the directory that contains wp-config.php).

The directory structure can vary per server, but often you need to descend only one directory to the public_html directory:

cd public_html

If this is not the case on your server, use the cd (“change directory”) command to go into the correct directory:

cd path/to/your/wp/folder

You can use the ls command again to check if you are in the proper directory. If not, you can always go back up one level with:

cd ..

Use the facetwp index command

When you have landed in the right directory, you can use any of the below indexing commands.

To trigger a full re-index, enter the wp facetwp index command:

wp facetwp index

If all is well, after a few seconds the re-indexing process will start. While the process is running, your terminal will display a live progress bar that looks like this:

yourusername$ wp facetwp index
Indexing:  47 % [==========>                          ] 0:14 / 0:30

Depending on the number of posts, products, product variations, terms, and facets, the indexing process can take anywhere from a few seconds or minutes to several hours. Once the indexing is completed, a success message will be displayed below the progress bar:

yourusername$ wp facetwp index
Indexing:  100% [=====================================] 0:30 / 0:30
Success: Indexing complete.

If you need to kill the indexing process, you can use ^C (CTRL-C).

You can also do a partial re-index by specifying post IDs, post types or facets. Or you can purge the index database table:

WP-CLI indexing commands

FacetWP provides 2 CLI commands. One for indexing:

wp facetwp index [--ids=<ids>] [--facets=<facets>]

and the other for purging the index database table:

wp facetwp purge [--ids=<ids>] [--facets=<facets>]

Parameters you can use for both commands:

  • ids (optional) — a comma-separated list of post IDs
  • facets (optional) — a comma-separated list of facet names

Command examples

wp facetwp index

Re-index the entire site:

wp facetwp index

Re-index a single post:

wp facetwp index --ids=42

Re-index a multiple posts:

wp facetwp index --ids=42,43,44

Re-index one or more facets, in this example the facets named “make” and “model”:

wp facetwp index --facets=make,model

Re-index all posts in a post type, in this example a post type named “cars”:

wp facetwp index --ids=$(wp post list --post_type=cars --format=ids | tr " " ",")

wp facetwp purge

Purge the index table:

wp facetwp purge

Purge values for a facet named “year”:

wp facetwp purge --facets=year

WP-CLI indexing with a cron job

The above indexing commands can also be run with a cron job on your server.

This approach gives you control over when the indexing process runs exactly, and at which frequency. You could for example schedule it to run in the middle of the night every other day. With this approach, it is advisable to turn off FacetWP’s automatic indexing with the facetwp_indexer_is_enabled hook.

There are a few ways you can set up a cron job for a WP-CLI indexing command:

Set an indexing cron job in your hosting panel

If you have access to a hosting panel, like cPanel or DirectAdmin, you can easily set up a cron job in its interface.

The following cron command will change the directory to the site’s main folder, and start a full re-index:

cd /path/to/your/wp/folder/; wp facetwp index

If you set this command to run at 02:00 AM every day, the cron job would look like this in cPanel:

Using WP-CLI indexing commands in a cron job in cPanel
Running a WP-CLI indexing command in a cron job in cPanel each night at 02:00 AM

Note: you can add >/dev/null 2>&1 after a command in a cron job to disable email notifications:

cd /path/to/your/wp/folder/; wp facetwp index >/dev/null 2>&1

Using a bash script

Another approach is to use a bash script. First, make a new shell script file called index.sh in the same directory as your WordPress install, and set its contents to:

#!/bin/bash
cd /path/to/your/wp/folder
wp facetwp index

You can then use the following cron command to execute the lines in the bash script:

sh /path/to/your/wp/folder/index.sh

Set an indexing cron job in terminal

If you don’t have access to a hosting panel to set cron jobs, or if you prefer using the terminal, you can set cron jobs with the crontab -e command.

Using the example bash script above, set to run at 02:00 AM every day, the cron job to add would look like this:

0 2 * * * sh /path/to/your/wp/folder/index.sh

The Crontab Generator can help with getting the syntax of the timing right.

See also