WP CLI commands

WP CLI is a command line interface to WordPress. It’s well-loved by developers and for a good reason, as it makes many common WordPress development tasks a breeze to do.

Relevanssi includes some tasks that can well be done from the command line and since adding new commands to WP CLI is very simple, Relevanssi Premium has WP CLI support from version 1.15.1 onwards.

This document describes the WP CLI commands available for Relevanssi. For the exact list of commands available on your installation of WordPress and Relevanssi, see wp help relevanssi.

wp relevanssi index

wp relevanssi index

Rebuilds the whole index. This is the fastest and easiest way to rebuild the index.

— post
If you want to index just a single post, define the post ID with this option.

— target=users|taxonomies
Set target to “users” to only rebuild the user profiles, and to “taxonomies” to only rebuild the taxonomy terms.

— extend=true|false
If for some reason you can’t rebuild the whole index at one go, setting this to true will continue the indexing from where it was left. The document limit will be taken from the settings page, unless the limit parameter is used.

— limit
Number of posts you want to index at one go. If you are extending the indexing, the relevanssi_index_limit option is used for limit if this value is left empty.

— index_debug=true|false
If you want to debug indexing issues, you can enable this setting and index just one post with the post option – this doesn’t do anything when indexing the whole database – to see the post as Relevanssi sees it.

Here’s how you can index all sites in your network:

wp site list --field=url | xargs -n1 -I % wp --url=% relevanssi index

wp relevanssi refresh

This is a helpful tool for big production sites. This command rebuilds the index (only posts at the moment, not taxonomy terms or users), without emptying the index first. This means the search will continue to work fine during the process. Relevanssi will go through every post and reindex it separately.

There may be limitations on how big database this command can handle since it needs to load in all the posts at once, but since the command only needs to load in the ID numbers of the posts, there should be no problems refreshing even hundreds of thousands of posts.

wp relevanssi truncate_index

This command is used to truncate the Relevanssi index.

wp relevanssi add_stopword

Adds stopwords to the list of stopwords and removes those words from the index. Just list any number of stopwords after the command, separated with spaces.

wp relevanssi remove_stopword

Removes stopwords from the list of stopwords. This can’t return the stopwords back to the index, so remember to rebuild the index after removing the stopwords.

wp relevanssi reset_log

Empties out the wp_relevanssi_log database table. Use with caution, you can’t undo this command.

wp relevanssi common

Shows the 25 most common words in the index. These are prime stopword candidates.

— limit
The number of words to display. Defaults to 25.

wp relevanssi read_attachments

Reads the attachment content for all attachments that haven’t been read yet.

wp relevanssi remove_attachment_content

Deletes all attachment content from the posts (ie. removes all _relevanssi_pdf_content and _relevanssi_pdf_error custom fields). Use with caution! Does not reindex or deindex posts, so you need to run wp relevanssi index or wp relevanssi refresh after this.

wp relevanssi remove_attachment_errors

Deletes all attachment errors from the posts (ie. removes all _relevanssi_pdf_error custom fields).

wp relevanssi regenerate_related

Flushes all caches for related posts and regenerates new related posts for all posts.

Multisite users

If you are running a multisite setup, remember that you need to use the --url parameter to define which site you want to access.

Suggestions for commands?

I’m interested in developing the WP CLI support further. These were my first ideas of what can be done with Relevanssi Premium and WP CLI. If you have any suggestions for possible commands, please comment on this post.