How to execute an "index ALL" action via php script

I have a cronjob that runs 3 times daily …

The cronjob creates new posts (modified) in wordpress and delete the old ones.

Then i have manually to trigger the action button “Re-index” via Customized PlugIn, so that attributes values in Algolia are not “null”.

How can i trigger the “Re-index” ALL action at last in my php script ?

It is important that it is “ALL”… any code available ?

Hi there,

We provide the code for a reindexing command in our documentation. This command will perform a complete reindex when called. WordPress exposes a method to run WP CLI commands. You could use this method to call the reindexing command at the end of your cronjob, like this:

WP_CLI::runcommand( 'algolia reindex')

I hope this helps!

But i don’t use command prompt for running the cronjob, is it a problem ?

Sorry, my code example may not have been too clear. It’s PHP code, which you can run in your WordPress installation.

<?php

// Your script here...
WP_CLI::runcommand('algolia reindex')

I assumed you’re running a PHP script in your cronjob, please let me know if that’s not the case.

To ensure your index is not down while you’re updating all your records, you may want to have a look at zero-downtime indexing in WordPress. This guide will also show you how to reindex your data.

Let me know if you have any more questions!

Cheers,
Devin.

1 Like

My cronjob is located in another folder outside the Algolia plugin with the wp-cli.php

Which file shall i require/inlcude in my cronjob to run this command?

Once you’ve added the command to the WP_CLI, you should be able to call the command as you would with any other WP_CLI command. You may need to consult the WordPress API Guide.