Issue with Adding Custom Meta Fields to Typesense Index in WordPress

TLDR Svitlana experienced errors when adding custom meta fields to Typesense index for WordPress. Digamber advised ensuring image URLs or empty strings are being sent to the index.

Powered by Struct AI
Join the chat
May 12, 2022 (20 months ago)
Photo of md5-76fad6f81d16ed9e9943a42dc7201f4c
01:10 PM
Hi! We are using Search with Typesense for WordPress and trying to add custom meta fields to the index. But as soon as I add a custom field to the ‘cm_typesense_schema’ filter, the index always returns empty. Happens with the custom post types and default posts.

Here is my code for the schema:
/*** Adds the post type book under available post_types ***/
function cm_typesense_add_available_post_types( $available_post_types ) {
    $available_post_types['style_card'] = [ 'label' => 'Style Cards', 'value' => 'style_card' ];

    return $available_post_types;
add_filter( 'cm_typesense_available_index_types',  'cm_typesense_add_available_post_types');

//only necessary if the default post schema is not necessary
function cm_typesense_style_card_schema( $schema, $name ) {
    if ( $name == 'style_card' ) {
        $schema = [
            'name'                  => 'style_card',
            'fields'                => [
                [ 'name' => 'post_title', 'type' => 'string' ],
                [ 'name' => 'post_author', 'type' => 'string' ],
                [ 'name' => 'post_date', 'type' => 'string' ],
                   [ 'name' => 'post_id', 'type' => 'string' ],
                [ 'name' => 'post_thumbnail', 'type' => 'string' ],
            [ 'name' => 'preview_image_url', 'type' => 'string' ], // custom field

    return $schema;

add_filter( 'cm_typesense_schema', 'cm_typesense_add_style_card_schema', 10, 2 );

The index works if I remove [ 'name' => 'preview_image_url', 'type' => 'string' ] and leave only the default WordPress fields.
Is there a way to add custom fields to the index and make it work? Thanks.
Photo of md5-8813087cccc512313602b6d9f9ece19f
01:10 PM
Digamber ^
Photo of md5-a0246423746b3b51425d05cfd9c494ae
03:40 PM
Hi Svitlana - make sure you are either sending an empty string or an actual image url to the index otherwise it fails.
Also - please turn on debug log and check the logs it should provide more insight,