Posted on

Indexing with a cron job

I have some trouble with reindexing of new posts. The reason is simple – I sync data into the database automatically.

So what I need is a cronjob to reindex the site once a day. How do I do a cronjob?

The easiest way would be to get Relevanssi Premium and use WP CLI, so you can just say wp relevanssi index to get the job done.

If that’s not possible, you need to use the function relevanssi_build_index() to build the index. It’ll work fine if your database is small enough so that you can index it in one go; if not, you need to run it several times, first with

relevanssi_build_index(false, false, X);

then

relevanssi_build_index(true, false, X);

as many times as is necessary, where X is the number of posts you are guaranteed to be able to index at one go.

Creating URLs for the cron job

To make this happen in practice, you need two files. First, create a file relevanssi-start-indexing.php with this content:

<?php
require 'wp-blog-header.php';
relevanssi_build_index( false, false, 100 );

and then a file relevanssi-continue-indexing.php with this content:

<?php
require 'wp-blog-header.php';
relevanssi_build_index( true, false, 100 );

This assumes the files are in your WP installation root directory. If they’re not, adjust the path to wp-blog-header.php so that it points to the right place.

Now to start indexing, have the cron job visit the relevanssi-start-indexing.php – this will start the indexing and will index 100 posts. Then have the cron job visit relevanssi-continue-indexing.php as many times as is necessary to have everything indexed.

You don’t have to do this 100 posts at a time, but you need to adjust the number based on your site: how many posts can be indexed before PHP execution times out? It’s likely something like 500, but the exact number depends on your posts and server.

Removing automatic indexing

If you index with a cron job, you may want to disable automatic indexing. You can do it by adding this to your site:

remove_action( 'wp_insert_post', 'relevanssi_insert_edit', 99 );
remove_action( 'wp_after_insert_post', 'relevanssi_insert_edit', 99 ); // Relevanssi 4.17 / Premium 2.19 onwards.

This was asked at the WordPress.org support forums.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *