🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Integrations / WordPress

Algolia doesn’t provide support for WordPress or any WordPress plugin.

In the previous section, you defined a new post_to_record filter in the reindex_post method. You can use this to define how to convert a post into a record before sending it to Algolia.

You technically could define how to serialize posts in the plugin. Yet, since some content depends on the theme—thumbnail size, for example—it might be a better idea to do it directly in the theme. It also works better because most themes with custom post types embed that feature in the theme, not in a separate plugin.

It’s highly recommended to create a child theme instead of modifying the original theme directly. This way, you can keep on installing updates.

Converting posts into records

In your theme’s functions.php file, you can define a new function and add it to your new WordPress filter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function algolia_post_to_record(WP_Post $post) {
    $tags = array_map(function (WP_Term $term) {
        return $term->name;
    }, wp_get_post_terms($post->ID, 'post_tag'));

    return [
        'objectID' => implode('#', [$post->post_type, $post->ID]),
        'title' => $post->post_title,
        'author' => [
            'id' => $post->post_author,
            'name' => get_user_by('ID', $post->post_author)->display_name,
        ],
        'excerpt' => $post->post_excerpt,
        'content' => strip_tags($post->post_content),
        'tags' => $tags,
        'url' => get_post_permalink($post->ID),
        'custom_field' => get_post_meta($post->id, 'custom_field_name'),
    ];
}
add_filter('post_to_record', 'algolia_post_to_record');

The preceding example is a naive implementation that performs several SQL queries. This isn’t an issue, but could become one if you have thousands of posts. In this case, make sure to adapt the function.

Handling long articles

If you have long articles, you might hit the allowed size limit per Algolia record. This limit exists for relevance and performance reasons.

If you hit this limit and get an error, please refer to the advanced guide to see how to split your content into several records.

Did you find this page helpful?