-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-cli.php
56 lines (41 loc) · 1.15 KB
/
wp-cli.php
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
51
52
53
54
55
56
<?php
if (!(defined('WP_CLI') && WP_CLI)) {
return;
}
class Algolia_Command {
public function reindex_post($args, $assoc_args) {
global $algolia;
$index = $algolia->initIndex('<name of your Algolia Index>');
$index->clearObjects()->wait();
$paged = 1;
$count = 0;
do {
$posts = new WP_Query([
'posts_per_page' => 100,
'paged' => $paged,
'post_type' => 'post',
'post_status' => 'publish'
]);
if (!$posts->have_posts()) {
break;
}
$records = [];
foreach ($posts->posts as $post) {
if ($assoc_args['verbose']) {
WP_CLI::line('Serializing ['.$post->post_title.']');
}
$split = apply_filters('post_to_record', $post);
$records = array_merge($records, $split);
$count++;
}
if ($assoc_args['verbose']) {
WP_CLI::line('Sending batch');
}
$index->saveObjects($records);
$paged++;
} while (true);
WP_CLI::success("$count posts indexed in Algolia");
}
}
WP_CLI::add_command('algolia', 'Algolia_Command');
?>