save_post
Hook in WPFTS
The save_post
hook is a standard WordPress hook that triggers every time a post or page is saved or updated. WPFTS utilizes this hook to maintain the currency of its search index by re-indexing the content of the modified or newly created post.
What WPFTS does when save_post
is called:
-
Post Synchronization: WPFTS first calls the
MakePostsSync(true)
method to ensure that the internal list of posts requiring indexing is up-to-date. This guarantees that the post that has just been saved is included in the list for indexing. -
Setting Re-indexing Priority: The plugin executes the SQL query
update wpftsi_index set force_rebuild = 2 where tid = ...
to set a high priority for re-indexing the post. This ensures that this post is processed by the indexer first. -
Resetting the Status Timer:
set_option('status_next_ts', 0)
is called to reset the indexing status timer. This leads to the indexing status being recalculated on the next status request, and the user will see up-to-date information about the indexing process. -
Interrupting the Current Indexing Iteration:
set_option('is_break_loop', 1)
sets a flag to interrupt the current indexing cycle. This allows the indexer to start a new iteration and process the high-priority post. -
Starting the Indexer: WPFTS calls the
CallIndexerStartNoBlocking()
method to start the indexer in the background.
Important Functions Involved in save_post
Handling:
-
WPFTS_Core::MakePostsSync(true)
-
WPFTS_Core::set_option()
-
WPFTS_Core::CallIndexerStartNoBlocking()
-
wpfts_post_reindex()
(called insave_post
action)
How to Use This in Addon Development:
Understanding how WPFTS uses the save_post
hook allows addon developers to effectively integrate their data into the search index. For example, if your addon adds custom meta fields, you can use the wpfts_index_post
filter to add the values of these fields to $chunks
when indexing the post. Since WPFTS automatically re-indexes posts upon saving, your addon’s data will also be updated in the index.
Additional Notes:
-
WPFTS uses a non-blocking indexer start to avoid slowing down the post saving process.
-
Re-indexing happens in the background, so the user can continue working with the site without delays.