Automatic Updates
Now that we have indexed all the existing content, we want to keep our Algolia index up to date. Every time a post is added, updated, or deleted, we want to make sure to send this modification to Algolia.
Keeping Posts up to date
Every time a post is updated, it’s passed to the save_post
action.
It’s important to note that WordPress has an auto-save feature, so every few seconds the post is saved as a revision. We don’t want to update algolia until the “update” button is hit.
Four arguments are passed to the add_action
method. The fourth one is the number of arguments you
want the function to receive. In this case we want the three possible arguments. By default,
all additional arguments are not passed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function algolia_update_post($id, WP_Post $post, $update) {
if (wp_is_post_revision( $id) || wp_is_post_autosave( $id )) {
return $post;
}
global $algolia;
$record = (array) apply_filters($post->post_type.'_to_record', $post);
if (! isset($record['objectID'])) {
$record['objectID'] = implode('#', [$post->post_type, $post->ID]);
}
$index = $algolia->initIndex(
apply_filters('algolia_index_name', $post->post_type)
);
$index->saveObject($record);
return $post;
}
add_action('save_post', 'algolia_update_post', 10, 3);
Updating on post meta updates
If you are updating metadata and press the publish or update button in your post edit admin page,
it will trigger a reindex of the whole object. But in case you only modify a metadata entry, you
may need to add a method on the algolia_post_meta
action. This situation could also happen if
another plugin enriches your posts for instance.
In the following, we only update the correct attributes in Algolia, without sending the whole record.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function algolia_update_post_meta($meta_id, $object_id, $meta_key, $_meta_value) {
global $algolia;
$indexedMetaKeys = ['seo_description', 'seo_title'];
if (in_array($meta_key, $indexedMetaKeys)) {
$index = $algolia->initIndex(
apply_filters('algolia_index_name', 'post')
);
$index->partialUpdateObject([
'objectID' => 'post#'.$object_id,
$meta_key => $_meta_value,
]);
}
}
add_action('update_post_meta', 'algolia_update_post_meta', 10, 4);
Removing records on post deletion
The previous code will ensure a post is always up to date in Algolia. However, we
want to remove the record of a post when the post is deleted. In the same function, check if
the status of the post is trashed
. If this is the case, call the deleteObject
method instead.
The final function will look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function algolia_update_post($id, WP_Post $post, $update) {
if (wp_is_post_revision( $id) || wp_is_post_autosave( $id )) {
return $post;
}
global $algolia;
$record = (array) apply_filters($post->post_type.'_to_record', $post);
if (! isset($record['objectID'])) {
$record['objectID'] = implode('#', [$post->post_type, $post->ID]);
}
$index = $algolia->initIndex(
apply_filters('algolia_index_name', $post->post_type)
);
if ('trash' == $post->status) {
$index->deleteObject($record['objectID']);
} else {
$index->saveObject($record);
}
return $post;
}
add_action('save_post', 'algolia_update_post', 10, 3);
Handling split records
If you started splitting your records, please make sure you read our guide on how to split your records.