WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
138 stars 54 forks source link

Move content save action listener from `save_post` to `wp_after_insert_post` #384

Closed tw2113 closed 7 months ago

tw2113 commented 9 months ago

The save_post hook works just fine and dandy with our default data. However, it is running before term and meta data has been saved.

This a potential issue because say I do the following.

  1. Set a custom field to be a filtered-in shared attribute
  2. Start a post
  3. Add some content
  4. Add a meta field with the specified key for shared attribute
  5. Publish the post.

The post gets pushed to the Algolia index, the attribute will get created, but the attribute is empty. This is because the meta had not been saved yet. Saving the post again without making any edits, repeats the process but this time the property is set to the saved value, because it was available.

If we were using the wp_after_insert_post hook for our listening, following the same steps would produce the results of having the property set correctly the first time, because we did the index sync after the meta has been saved to the database.

Right now, the workaround would be to add each meta key you want to get sync'd the first time, to the algolia_watch_post_meta_keys filter, of which the callback does it's own post sync, which runs on these actions: added_post_meta, updated_post_meta, deleted_post_meta

However, I want to make sure we're not risking some sort of backwards compatibility issues.

tw2113 commented 9 months ago

Positive evidence around this being a good change to potentially make:

https://wordpress.org/support/topic/auto-index-new-posts-and-updated-posts/

kory-northrop commented 9 months ago

As a user of this plugin, I'd advocate for the proposed change. We were having some issues with some of our custom metadata being successfully attributed to our index records with the post watcher indexing records via the save_post hook, but things worked as desired when using wp_after_insert_post instead. I don't know what kind of consequences would result from making the change, but if it seems innocuous then I give two enthusiastic thumbs up!

tw2113 commented 8 months ago

PR ready for review https://github.com/WebDevStudios/wp-search-with-algolia/pull/388

tw2113 commented 7 months ago

This got deployed as part of 2.7.0.