I have ACF fields on my post/page screens which add advance content fields, so it updates the post_content
field with correct html formatting.
If you remove the first $post_type === ...
conditional check, it would cause Algolia to not update it's task content which is obvious that this hook will run for every post type
functions.php
<?php
function my_update_body($post_id) {
global $post_type;
// remove this check to make Algolia sad :(
if ($post_type !== 'post' || $post_type !== 'page') {
return;
}
$_post_id = is_object($post_id) ? $post_id->ID : $post_id;
// disable to avoid infinite loops
remove_action('save_post', 'my_update_body');
remove_action('pre_post_update', 'my_update_body');
remove_action('edit_page_form', 'my_update_body');
$new_post = array();
$new_post['ID'] = $_post_id;
// get_flexible_content here wraps the formatted html of each acf field content
$new_post['post_content'] = get_flexible_content($_post_id);
wp_update_post($new_post);
// re-enable
add_action('save_post', 'my_update_body');
add_action('pre_post_update', 'my_update_body');
add_action('edit_page_form', 'my_update_body');
}
add_action('save_post', 'my_update_body');
add_action('pre_post_update', 'my_update_body');
add_action('edit_page_form', 'my_update_body');