Version 4 Documentation

How to Integrate with Content Imports/Migrations

Note: This documentation is for SearchWP 4
SearchWP 3.x: How to Best Work 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
Tell SearchWP to purge each imported entry during the import (optional)
Run import in full
Enable SearchWP indexer
Trigger 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. If however your import is changing data that is not indexed, you can save time/resources by skipping the index rebuild.

All hooks should be added to your custom SearchWP Customizations Plugin.

* WP All Import SearchWP PRE-import routine
* @link
add_action( 'pmxi_before_xml_import', function( $import_id ) {
// If you do not want SearchWP to process all of the edits
// made by the import process, uncomment the following hook.
// add_filter( 'searchwp\sources', '__return_empty_array', 99999 );
}, 10 );
* WP All Import callback that runs for each imported entry.
* Purge that post from the SearchWP index.
* NOTE: This is not necessary unless you have uncommented the hook
* in the pmxi_before_xml_import action above.
* @link
add_action('pmxi_saved_post', function( $post_id ) {
$source_name = \SearchWP\Utils::get_post_type_source_name( get_post_type( $post_id ) );
$source = \SearchWP::$index->get_source_by_name( $source_name );
\SearchWP::$index->drop( $source, $post_id, true );
}, 10 );
* WP All Import SearchWP POST-import routine
* @link
add_action( 'pmxi_after_xml_import', function( $import_id ) {
// If you uncommented the hook in the pmxi_before_xml_import
// action above, uncomment the following hook.
// remove_filter( 'searchwp\sources', '__return_empty_array', 99999 );
}, 10 );

With this snippet 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 import is processing.