Integrations / Platforms / WordPress / Importing Existing Content

Importing Existing Content

When integrating Algolia for the first time, you probably want to import all your existing content. The following guide shows you how to index posts. You can also reuse it to index any other custom post types or terms.

Algolia setup command

First, let’s create a custom WP-CLI command to trigger an Algolia reindex manually. You want to namespace all Algolia commands under the algolia namespace so that you can call them this way:

1
wp algolia reindex

The class name defines the namespace. Create a new wp-cli.php file at the root of the plugin, and add the following code:

1
2
3
4
5
6
7
8
9
10
11
<?php

if (!(defined('WP_CLI') && WP_CLI)) {
    return;
}

class Algolia_Command {

}

WP_CLI::add_command('algolia', 'Algolia_Command');

You can now include this new file in the main algolia-custom-integration.php plugin file. Add the following line at the end:

1
require_once __DIR__ . '/wp-cli.php';

Adding a reindex command

To add a new command, you can define a new public method in the Algolia_Command class. The next command indexes all posts.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public function reindex_post($args, $assoc_args) {
    global $algolia;
    $index = $algolia->initIndex('index_name');

    $index->clearObjects()->wait();

    $paged = 1;
    $count = 0;

    do {
        $posts = new WP_Query([
            'posts_per_page' => 100,
            'paged' => $paged,
            'post_type' => 'post'
        ]);

        if (!$posts->have_posts()) {
            break;
        }

        $records = [];

        foreach ($posts->posts as $post) {
            if ($assoc_args['verbose']) {
                WP_CLI::line('Serializing ['.$post->post_title.']');
            }
            $record = (array) apply_filters('post_to_record', $post);

            if (!isset($record['objectID'])) {
                $record['objectID'] = implode('#', [$post->post_type, $post->ID]);
            }

            $records[] = $record;
            $count++;
        }

        if ($assoc_args['verbose']) {
            WP_CLI::line('Sending batch');
        }

        $index->saveObjects($records);

        $paged++;

    } while (true);

    WP_CLI::success("$count posts indexed in Algolia");
}

In your terminal, run the following command to index all your posts to Algolia:

1
wp algolia reindex_post

Note that we’re first clearing the index, then reindexing all the data. It means that for a short period, your Algolia index is empty or incomplete. You can fix this behavior by implementing zero-downtime reindexing.

Customizing the Algolia index name

Instead of hard coding the Algolia index name in the plugin, you can create a new filter. This way, you can customize the index name from your theme.

In the reindex_post method, replace the index initialization with:

1
2
3
$index = $algolia->initIndex(
    apply_filters('algolia_index_name', 'post')
);

You can register a new filter in your theme’s functions.php to customize the Algolia index name. In the following example, we prefix the default index name with the database table prefix.

1
2
3
4
5
6
function algolia_post_index_name($defaultName) {
    global $table_prefix;

    return $table_prefix.$defaultName;
}
add_filter('algolia_index_name', 'algolia_post_index_name');

Indexing only a subset of posts

We’re currently sending all posts into Algolia. If you only want to send a subset, you can modify the arguments passed to WP_Query in the algolia_reindex method. In the following example, we only index the published posts.

1
2
3
4
5
6
$posts = new WP_Query([
    'posts_per_page' => 100,
    'paged' => $paged,
    'post_type' => 'post',
    'post_status' => 'publish',
]);

For now, we’re hard coding the post type, therefore only indexing posts. Later on, we’ll make the post type dynamic so we can also index custom post types.

Did you find this page helpful?