Manage Relevanssi directly from the command line
WP CLI Commands
WP CLI is a command-line interface to WordPress. It’s well-loved by developers for a good reason, as it makes many common WordPress development tasks a breeze.
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 specific installation of WordPress and Relevanssi, run wp help relevanssi.
Relevanssi WP CLI commands in actionwp relevanssi index
Rebuilds the whole index. This is the fastest and easiest way to rebuild the index.
Options:
--post: If you want to index just a single post, define the post ID with this option.--target=users|taxonomies: Set the target to “users” to only rebuild user profiles, or “taxonomies” to only rebuild taxonomy terms.--extend=true|false: If you can’t rebuild the whole index at one go, setting this to true will continue the indexing from where it left off. The document limit will be taken from the settings page unless the limit parameter is used.--limit: The number of posts you want to index at one go. If you are extending the indexing, therelevanssi_index_limitoption is used if this value is left empty.--index_debug=true|false: If you want to debug indexing issues, enable this setting and index just one post using the--postoption. (This doesn’t do anything when indexing the whole database). This outputs the post exactly as Relevanssi sees it.
Here’s how you can index all sites in your multisite network at once:
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 a database this command can handle since it needs to load in all the posts at once, but since the command only needs to load 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 instantly truncate (empty) 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 by spaces.
wp relevanssi remove_stopword
Removes stopwords from the list of stopwords. This cannot automatically return the stopwords back to the index, so remember to rebuild the index after removing them.
wp relevanssi reset_log
wp_relevanssi_log database table. Use with caution; you cannot undo this command.wp relevanssi common
Shows the 25 most common words in the index. These are prime stopword candidates.
Options:
--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
_relevanssi_pdf_content and _relevanssi_pdf_error custom fields). Use with caution! This does not reindex or deindex posts, so you need to run wp relevanssi index or wp relevanssi refresh after running this.wp relevanssi remove_attachment_errors
Deletes all attachment errors from the posts (i.e., 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.
wp relevanssi list_pinned_posts
Lists all pinned or unpinned posts, or both.
Options:
--format: The format used to display the results. Possible values are “table”, “json”, “csv”, “yaml”, and “count”. Defaults to “table”.--type: The type of results to list. Possible values are “pinned”, “unpinned”, and “both”. Defaults to “both”.
wp relevanssi list
Lists posts that either are in the index or are not in the index.
Options:
--format: The format used to display the results. Possible values are “table”, “json”, “csv”, “yaml”, and “count”. Defaults to “table”.--type: The type of results to list. Possible values are “post”, “term”, and “user”. Defaults to “post”, which includes all post types, not just standard posts.--post_type: Post type to include in the results. Defaults to none, which shows all post types.--status: Status of the posts to show. Possible values are “indexed” and “unindexed”. Defaults to “indexed”.
If you are running a multisite setup, remember that you need to use the
--url parameter to define which site you want to access.I’m interested in developing the WP CLI support further. Please reach out or comment if you have any suggestions for possible commands.