Can you please help in sorting search results based on custom field?
Swaraj088 opened this issue · comments
Hi
I am testing the functionality of the plugin in the test environment, I have indexed records along with a custom field to add it as an attribute.
// custom field to Algolia for all post types
add_filter('algolia_post_shared_attributes', 'start_date_to_algolia', 10, 2);
/**
* Add the 'start_date' field to Algolia attributes.
*
* @param array $attributes
* @param WP_Post $post
* @return array
*/
function start_date_to_algolia(array $attributes, WP_Post $post)
{
if ( 'webinars' !== $post->post_type ) {
// here if the post isn't a 'webinars', so we return the attributes unaltered.
return $attributes;
}
// if the ACF field exists for the post
if (function_exists('get_field') && get_field('start_date', $post->ID)) {
$attributes['start_date'] = get_field('start_date', $post->ID);
}
return $attributes;
}
Now I want to get results sorted using the start_date attribute, is there any way? Can you please guide how we can do that?
start_date is indexed as a timestamp in the index records! For example, start_date: 1706250600
Thank you so much
I would check out:
- https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/
- https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/
Most specifically, from what I'm reading, you'll need to get a standard replica set up with this new sorting strategy added
Give that a lookover and see what you make of it, and if you still have some more questions, we'll do what we can to help with those.
Closing the issue as a general support, but feel free to comment further as needed.