When is the WordPress algolia_post_shared_attributes hook called?

As far as I can tell the algolia_post_shared_attributes hook in your WordPress plugin is called just before the post is saved.

This is not good when you want to update the attributes with something that was changed on the post, because any of the values we get will be from the “old” post version and not the “new” version which hasn’t been saved yet.

For example:

function my_post_shared_attributes( array $shared_attributes, WP_Post $post) {

	$custom_field_value = get_post_meta( $post->ID, 'custom_field_name' )

    $shared_attributes['custom_field_name'] = $custom_field_value;

    return $shared_attributes;
}
add_filter( 'algolia_post_shared_attributes', 'my_post_shared_attributes', 10, 2 );

Do you see the problem?

Whatever is inside $custom_field_value is the value the custom field had BEFORE the post was saved.

It doesn’t contain the new value we are trying to push, because the post hasn’t been saved yet with those new custom field values. We can’t retrieve the new value because it is not saved to the post yet.

So what we need is another hook for updating attributes that fires AFTER the post was saved. That way any of the values we retrieve and push to Algolia will contain the new updated values.

Does such a hook exist?

Can you include one?

Or do we need to hack the plugin to get it working?

Have discovered you need to manually click the “re-index page records” button under Algolia Search > Search Page settings to get the new values pushed into the index.

This is ok for our current work flow, but in the future it would be nice to have a programmatic way to achieve this :slight_smile:

1 Like