Last active
June 8, 2022 10:07
-
-
Save sachyya/77d35798a87e590ed43210830760e67e to your computer and use it in GitHub Desktop.
Index posts' tags in Typesense
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'cm_typesense_schema', 'your_slug_add_tags', 10, 2 ); | |
function your_slug_add_tags( $schema, $name ) { | |
if( 'post' === $name ) { | |
$schema['fields'][] = [ 'name' => 'tags', 'type' => 'string[]', 'facet' => true ]; | |
} | |
return $schema; | |
} | |
add_filter( 'cm_typesense_data_before_entry', 'your_slug_tags_data', 10, 4 ); | |
function your_slug_tags_data( $formatted_data, $raw_data, $object_id, $schema_name ) { | |
if( 'post' === $schema_name ) { | |
$tags = get_the_tags( $raw_data->ID ); | |
$tag_names = []; | |
if ( ! empty( $tags ) ) { | |
foreach ( $tags as $tag ) { | |
$tag_names[] = html_entity_decode( $tag->name ); | |
} | |
} | |
$formatted_data['tags'] = is_array( $tag_names ) && ! empty( $tag_names ) ? array_values( array_unique( $tag_names ) ) : []; | |
} | |
return $formatted_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment