Skip to content

rangeSlider - how to customize the displayed value ? #13

@cfab

Description

@cfab

Hello,
I'm not sure if I set up this correctly but I would like to have a facet that allows to search within the posts by publication date
So here under is what I did (I just kept one other facet as example (lcr_post_theme which is a custom taxonomy). It works, my lcr_post_theme facet and the slider, but the displayed value of the rangeSlider are unix timestamp, how can display maybe only the year, ofrbetter, the full date in a format like this: 2024-08-26
Here is the code, the settings and visual result...
Any help welcome !

add_filter('cm_typesense_schema', function ($schema, $name) {
           if ($name == 'post') {
               $schema['fields'][] = ['name' => 'lcr_post_theme', 'type' => 'string[]', 'optional' => true, 'facet' => true];
               // add a date range filter
               $schema['fields'][] = ['name' => 'publication_date', 'type' => 'int64', 'optional' => true, 'facet' => true];
           }

           return $schema;
       }, 10, 2);

       add_filter('cm_typesense_data_before_entry', function ($formatted_data, $raw_data, $object_id, $schema_name) {
           if ($schema_name == 'post') {
               // theme
               $lcr_post_theme = [];
               $terms = get_the_terms($object_id, 'lcr_post_theme');
               if ($terms) {
                   foreach ($terms as $term) {
                       $lcr_post_theme[] = $term->name;
                   }
               }

               // date should be an int64
               $formatted_data['publication_date'] = strtotime(get_the_date('Y-m-d', $object_id));
               // $formatted_data['publication_date'] = get_the_date('Y-m-d', $object_id); // -> this creates an error while indexing
           }

           return $formatted_data;
       }, 10, 4);

       /** Add the genre option to available index list **/

       add_filter('cm_typesense_available_index_types', function ($available_post_types) {
           $available_post_types['lcr_post_theme'] = ['label' => 'Thèmes', 'value' => 'lcr_post_theme', 'type' => 'taxonomy'];
           // not sure if this needed
           $available_post_types['publication_date'] = ['label' => 'Date de publication', 'value' => 'publication_date', 'type' => 'date'];

           return $available_post_types;
       });

       add_filter('cm_typesense_enabled_taxonomy_for_post_type', function ($taxonomies) {
           $taxonomies[] = 'lcr_post_theme';
           // not sure if this is needed as well
           $taxonomies[] = 'publication_date';

           return $taxonomies;
       });

       add_filter('cm_typesense_filter_type', function ($filterType, $filterName) {
           if ($filterName == 'publication_date') {
               $filterType = 'rangeSlider';
           }

           return $filterType;
       }, 10, 2);



       // add_filter('cm_typesense_search_facet_settings', function ($facet_settings, $facet) {
       //     if ($facet == 'publication_date') {
       // I guess it is here that I may customize somethin gbut how ??

       //     }

       //     return $facet_settings;
       // }, 10, 2);

Capture 2024-08-26 à 11 24 32
Capture 2024-08-26 à 11 27 20

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions