Integrations / Platforms / WordPress / Importing Existing Content
Mar. 18, 2019

Importing Existing Content

When integrating Algolia you probably want to import all your existing content at once. The following parts show how to index posts and can be reused to index any other custom post types or terms.

Setup Algolia command

We’re going to create a custom command for wp-cli, in order to manually call a reindex operation. All Algolia commands will live under the algolia namespace, so they’ll be called this way:

1
wp algolia reindex

The name of the namespace is defined by the class name. Create the following class in a new algolia-custom-integration/wp-cli.php file.

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

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

class Algolia_Command {
    public function hello( $args, $assoc_args ) {
        WP_CLI::success('Algolia is correctly loaded 🎉');
    }
}

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

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

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

Head to your console and try the following command wp algolia hello. You should see a success message displayed.

Add reindex command

To add a command, define a new public method in the Algolia_Command class. The next command will index 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
public function reindex_post( $args, $assoc_args ) {
    global $algolia;
    $index = $algolia->initIndex('WP_TEST');

    $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");
}

By default, the whole content of a post will be indexed and searchable. No post meta are indexed yet. In the next section, we’ll see how to customize what post information to send to Algolia.

In your terminal, run the wp algolia reindex_post command to send all your posts to Algolia.

Command wp algolia reindex_posts

Note that we are clearing the index first, then reindexing all the data. It means that for a little period of time, your Algolia index will be empty or incomplete. There are ways to do zero-downtime reindexing with Algolia, please refer to the advanced section.

Customizing Algolia index name

Instead of hard coding the Algolia index name, we’ll create a new filter. This way, you can easily customize the index name from your theme.

For now, we’re hard coding the type post as an argument to the method. We’ll see in another section how to make the post type dynamic, in order to also index custom post types.

Replace the index initialization of the reindex_post method by:

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

Now, in your theme’s functions.php, feel free to register a new filter customizing the Algolia index name.

Here is an example of what you can do. We prefix the default index name (in this case, the “post” we just hard coded) 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

In the previous example, we send all posts from the database. If you want to only send a subset of your posts, you can modify the arguments passed to WP_Query in the algolia_reindex method. In this 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',
]);

Did you find this page helpful?