0

Here, I have some code which applies some indexing operations on an external service (Algolia). My purpose, is to index posts in a similar way, as described in their API documentation. For that cause, I have set up two action hooks:

add_action('save_post', 'algolia_save_post', 10, 3);

and

add_action('trashed_post','algolia_trash_post', 10, 1);

While this works as expected, I noticed that, in case metadata are altered, such as permalinks, the data are not updated in the index. I then tried the update_post_meta action hook:

add_action('update_post_meta', 'algolia_update_post_meta', 10, 4);

Which seems to do the job well. However, I suspect that update_post_meta is invoked both when metadata fields are altered, but also on save_post chain.

As I do not want to achieve a double, redundant indexing on article save I would like to ask, is the addition of the indexing code in the update_post_meta considered always on post save? In that case, I can remove the save_post hook and keep only this of meta. What I want to avoid, is the loss of index update in any case of the post update operations (excluding auto-save).

My local testing showed that, by adding the code only in the meta hook, everything works well and index gets updated on any field, yet I am not sure if there is an edge case I miss.

While this question involves the usage of an third-part vendor (Algolia), I believe it can be answered by a WordPress expert. Thank you in advance.

  • update_post_meta will always be called when post metadata changes. If you closely check algolia documentation, you would see they are only updating post fields (title, content, status) etc with save_post hook. And they are updating post meta data with update_post_meta hook. Post fields & post meta both are different type of data. – Shazzad Apr 8 at 16:33
  • @Shazzad I know both things are different and for a different purpose, but I want to know if update_post_meta is invoked within save_post action chain at some point. That way, I could attach the index update only in update_post_meta, in order to avoid redundant, duplicate index update. – Nick L. Apr 9 at 10:07
  • 1
    By default, the action hook update_post_meta does not get invoked within save_post hook. But, if you (or any plugin) add an action on save_post, and call update_post_meta() function inside that callback, then update_post_meta hook will be fired for each usage. – Shazzad Apr 9 at 18:09
  • Ok, if you are sure about this, please add it comprehensively as an answer so that I accept it. Thank you. – Nick L. Apr 9 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.