Integrations / Platforms / WordPress / Automatic Updates

Now that you’ve indexed all your existing content, you want to keep your Algolia index up to date. Every time you add, update, or delete content, you want to make sure to forward your changes to Algolia.

Keeping posts up to date

Every time you update a post, WordPress triggers the save_post action.

WordPress provides an add_action function which lets you bind a custom function to a specific action. You can leverage it to perform an Algolia update every time save_post triggers.

WordPress has an auto save feature that saves posts at a regular interval as a revision. You just want to reindex to Algolia when hitting the Update button.

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 records on meta updates

When you update a post’s metadata, you may want to reindex the changes instead of the entire record. To do so, you can add a new algolia_update_post_meta method and trigger it whenever WordPress calls the update_post_meta function.

The following example shows how to 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

Whenever you remove a post, you want to remove it from Algolia as well. In your algolia_update_post function, you can check if the post’s status is trash, and delete the object with the deleteObjects method if that’s the case.

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->post_status) {
        $index->deleteObject($record['objectID']);
    } else {
        $index->saveObject($record);
    }

    return $post;
}

add_action('save_post', 'algolia_update_post', 10, 3);
Did you find this page helpful?