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, 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, add a public function in your Algolia_Command
class in the wp-cli.php
file. The following 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
49
50
class Algolia_Command {
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 this first clears the index, then reindexes 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, the default index name is prefixed 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 a subset of posts
Your plugin is now sending all posts into Algolia. If you want to send a subset instead of all posts, you can change the arguments passed to WP_Query
in the algolia_reindex
method. The following example shows how to index just 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, the plugin is just indexing posts because the post
type is hard-coded. The next step is to make the post type dynamic so you can index custom post types.