SearchWP

This Documentation is for SearchWP Version 3

How to Best Work with Content Imports

Note: This documentation is for SearchWP 3.x
SearchWP 4: How to Integrate with Content Imports

SearchWP reacts to certain operations that happen within WordPress. When content is edited, WordPress tells SearchWP about the edit, and SearchWP in turn re-indexes that edited content. This keeps your index up to date over time automatically, without you having to manage anything.

By nature, content import routines (e.g. with WP All Import) make many (sometimes concurrent) edits in a very short timeframe. This can have adverse effects when it comes to SearchWP reacting to all of the signals being sent by WordPress that many edits are taking place.

If the import is big enough and runs fast enough, SearchWP’s indexer can become flooded with edit notifications and eventually the index will be out of sync. This is undesirable as SearchWP can begin cycling through its own index repeatedly or even worse returning inaccurate search results.

Properly handling content imports

It is recommended to at the very least disable the SearchWP indexer when running an import script. This can be done by modifying your own import script, or utilizing the hooks available in your chosen plugin. The pseudocode for what we’ll do is as follows:

Pause SearchWP indexer
Prevent SearchWP from listening to edit events (optional)
Purge SearchWP index (optional)
Alternatively, tell SearchWP to purge each imported entry during the import
Run import in full
Tell SearchWP to resume listening to edit events
Enable SearchWP indexer
Trigger initial build of SearchWP index

Note: Purging the index and ignoring edit events is optional, but recommended if your import routine is primarily responsible for editing existing content.

As of SearchWP 2.8.8 this process is quite easy. Here is a snippet to better integrate with hooks provided by WP All Import that accomplishes what was outlined in the pseudocode:

<?php
/**
* NOTE: THIS IS FOR SEARCHWP VERSION 3
*/
/**
* WP All Import SearchWP PRE-import routine
*
* @link https://searchwp.com/docs/kb/best-work-content-imports/
*/
function myswp_before_xml_import( $import_id ) {
// Pause the SearchWP indexer during import
SWP()->indexer_pause();
// Tell SearchWP to ignore edit events
searchwp_update_option( 'prevent_delta_triggers', true );
// Purge SearchWP index
// This is optional, but beneificial if your import process
// overwrites ALL content indexed by SearchWP.
// NOTE: if this is uncommented, the pmxi_saved_post action
// below should be commented out as it is not necessary.
// SWP()->purge_index();
}
add_action( 'pmxi_before_xml_import', 'myswp_before_xml_import', 10, 1 );
/**
* WP All Import callback that runs for each imported entry.
* Purge that post from the SearchWP index.
*
* NOTE: If you are purging the index in pmxi_before_xml_import above
* this hook should be disabled as it is not necssary.
*
* @link https://searchwp.com/docs/kb/best-work-content-imports/
*/
function myswp_post_updated( $post_id ){
SWP()->purge_post( $post_id );
}
add_action('pmxi_saved_post', 'myswp_post_updated', 10, 1 );
/**
* WP All Import SearchWP POST-import routine
*
* @link https://searchwp.com/docs/kb/best-work-content-imports/
*/
function myswp_after_xml_import( $import_id ) {
// Tell SearchWP to resume listening to edit triggers
searchwp_update_option( 'prevent_delta_triggers', false );
// Re-enable the SearchWP indexer
SWP()->indexer_unpause();
// Trigger the indexer to rebuild the index
SWP()->trigger_index();
}
add_action( 'pmxi_after_xml_import', 'myswp_after_xml_import', 10, 1 );
view raw functions.php hosted with ❤ by GitHub

With that code in place SearchWP will better react to your content imports, with an accurate index having been built after the import script has completed. This will also allow for your import to run a bit faster since SearchWP will not be competing for resources as the script is being run.

[wpforms id="3080"]