How to Index Custom Post Types

I’m creating a custom Algolia Integration for a Wordpress instance and there’s a custom post type called wpfc_sermon. I’m following the Algolia guide to getting all set up on a Wordpress instance, but I can’t figure out how to index the custom post type. Here’s the php that Algolia gives as a sample for indexing custom post types:

public function reindex_post($args, $assoc_args) {
global $algolia;
global $table_prefix;

$type = isset($assoc_args['type']) ? $assoc_args['type'] : 'post';

$indexName = $table_prefix.$type;
$index = $algolia->initIndex(
    apply_filters('algolia_index_name', $indexName, $type)
);
$index->clearObjects()->wait();

$paged = 1;
$count = 0;

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

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

    $records = [];

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

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

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

    $index->saveObjects($records);

    $paged++;

} while (true);

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

In command line via ssh I’m putting wp algolia reindex_post --type wpfc_sermon and it returns 0 1 entries indexed in Algolia . It crates an index, but it’s empty. If I just run wp algolia reindex_post then it successfully indexes all the default post types in my Wordpress instance.

What am I doing wrong?

Hi @website1, try wp algolia reindex_post --type=wpfc_sermon. Notice the equal sign between ‘–type’ and ‘wpfc_sermon’.

Let us know how it goes!