How to Best Work with Content Imports

Last updated December 18, 2018 « Knowledge Base

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)
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
/**
* 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
SWP()->purge_index();
}
add_action( 'pmxi_before_xml_import', 'myswp_before_xml_import', 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.

Fix Search on Your Site. No Coding Required!

Now you can utilize all of the content that's gone unrecognized by native WordPress search instantly with SearchWP

Get SearchWP