Skip to content

Instantly share code, notes, and snippets.

@sachyya
Created June 27, 2023 04:58
Show Gist options
  • Save sachyya/291d7470915c09cab73fa238b9e98c3b to your computer and use it in GitHub Desktop.
Save sachyya/291d7470915c09cab73fa238b9e98c3b to your computer and use it in GitHub Desktop.
Adding job location to TS server
<?php
function cm_typesense_add_available_post_types( $available_post_types ) {
$available_post_types['job_listing'] = [ 'label' => 'job_listing', 'value' => 'job_listing' ];
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_add_job_listing_schema( $schema, $name ) {
if ( $name == 'job_listing' ) {
$schema = [
'name' => 'job_listing',
'fields' => [
[ 'name' => 'post_content', 'type' => 'string' ],
[ 'name' => 'post_title', 'type' => 'string' ],
[ 'name' => 'post_type', 'type' => 'string' ],
[ 'name' => 'post_author', 'type' => 'string' ],
[ 'name' => 'comment_count', 'type' => 'int64' ],
[ 'name' => 'is_sticky', 'type' => 'int32' ],
[ 'name' => 'post_excerpt', 'type' => 'string' ],
[ 'name' => 'post_date', 'type' => 'string' ],
[ 'name' => 'post_id', 'type' => 'string' ],
[ 'name' => 'post_modified', 'type' => 'string' ],
[ 'name' => 'id', 'type' => 'string' ],
[ 'name' => 'permalink', 'type' => 'string' ],
[ 'name' => 'post_thumbnail', 'type' => 'string' ],
[ 'name' => '_job_location', 'type' => 'string' ]
],
'default_sorting_field' => 'comment_count'
];
}
return $schema;
}
add_filter( 'cm_typesense_schema', 'cm_typesense_add_job_listing_schema', 10, 2 );
function cm_typesense_format_job_data ( $formatted_data, $raw_data, $object_id, $schema_name ) {
if ( $schema_name == 'job_listing' ) {
// Replace the below function to retrieve the job location
$job_location = function_to_get_job_location();
$formatted_data['_job_location'] = $genres;
}
return $formatted_data;
}
add_filter( 'cm_typesense_data_before_entry', 'cm_typesense_format_job_data', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment