Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active July 13, 2020 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/501c08394e005179edfbfb9bd4e041fd to your computer and use it in GitHub Desktop.
Save jchristopher/501c08394e005179edfbfb9bd4e041fd to your computer and use it in GitHub Desktop.
Better control SearchWP (v3) during WP All Import import routine https://searchwp.com/docs/kb/best-work-content-imports/
<?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 );
@elvismdev
Copy link

@jchristopher

  • Does the SWP()->trigger_index() function immediately deletes the current existing index and starts from scratch to generate a brand new one?
  • If ^ true, does this means that while the index rebuild happens, the site search page might not return results accurately until the index rebuild finishes?

@mvdhoek1
Copy link

@jchristopher

  • Does the SWP()->trigger_index() function immediately deletes the current existing index and starts from scratch to generate a brand new one?
  • If ^ true, does this means that while the index rebuild happens, the site search page might not return results accurately until the index rebuild finishes?

Anyone has an answer? I'm eager to know the answers to this questions to.

@jchristopher
Copy link
Author

Sorry, GitHub didn't start notifying about comments on gists until somewhat recently so I did not see these questions.

Does the SWP()->trigger_index() function immediately deletes the current existing index and starts from scratch to generate a brand new one?

It does not delete the index, it only tells the indexer to check for unindexed content.

If ^ true, does this means that while the index rebuild happens, the site search page might not return results accurately until the index rebuild finishes?

Until the index has completed building, search results can be missing as the content is not indexed, that is correct. Unless you are working with a very large amount of content this "downtime" is minimal.

@mvdhoek1
Copy link

Thank you for your reply. Do you know how to create a new start index via code? I've tried several methods already:
SWP()->process_updates();
SWP()->update_index();
SWP()->trigger_index()

But still there are posts that cannot be found by searchWP.

@jchristopher
Copy link
Author

To receive support please open a support ticket using the Support tab of the SearchWP settings screen, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment