Skip to content

Instantly share code, notes, and snippets.

@solepixel
Last active October 27, 2020 04:35
Show Gist options
  • Save solepixel/794688ecb1667afc06a9930afa9dae76 to your computer and use it in GitHub Desktop.
Save solepixel/794688ecb1667afc06a9930afa9dae76 to your computer and use it in GitHub Desktop.
Custom parameters for Algolia search in WordPress
<?php
add_filter( 'algolia_terms_index_settings', '__return_empty_array' );
add_filter( 'algolia_users_index_settings', '__return_empty_array' );
add_filter( 'algolia_posts_listing_index_settings', 'mytheme_algolia_listing_settings' );
function mytheme_algolia_listing_settings( $settings ){
$numeric_fields = array_keys( mytheme_algolia_faceted_fields( array( 'int', 'float' ) ) );
$string_fields = array_keys( mytheme_algolia_faceted_fields( 'string' ) );
$settings = array(
'attributesToIndex' => array(
'unordered(post_title)',
'unordered(content)'
),
'customRanking' => array(
'desc(post_date)',
'desc(price)',
'desc(city)',
),
'attributeForDistinct' => 'post_id',
'distinct' => true,
'numericAttributesForFiltering' => $numeric_fields,
'attributesForFaceting' => $string_fields,
'attributesToSnippet' => array(
'post_title:30',
'content:30',
),
'snippetEllipsisText' => '…',
);
return $settings;
}
add_filter( 'algolia_post_listing_shared_attributes', 'mytheme_algolia_listing_custom_meta', 10, 2 );
function mytheme_algolia_listing_custom_meta( array $shared_attributes, WP_Post $post ) {
$remove_fields = array(
'comment_count',
'menu_order',
'post_author',
'post_type_label',
'images',
'post_mime_type',
'taxonomies',
'taxonomies_hierarchical',
'is_sticky',
'alt',
'metadata'
);
foreach( $remove_fields as $field ){
if( isset( $shared_attributes[ $field ] ) )
unset( $shared_attributes[ $field ] );
}
$listing = new MY_Listing( $post->ID );
if( ! $listing )
return $shared_attributes;
$searchable_fields = mytheme_algolia_faceted_fields();
foreach( $searchable_fields as $field => $type ){
$method = 'get_' . $field;
$meta_key = '_listing_' . $field;
if( $value = $listing->_get_field( $field ) ){ // this doesn't work :(
settype( $value, $type );
$shared_attributes[ $field ] = $value;
} elseif( $value = $listing->$method() ) { // this doesn't work :(
settype( $value, $type );
$shared_attributes[ $field ] = $value;
} elseif( $value = get_post_meta( $post->ID, $meta_key, true ) ) {
settype( $value, $type );
$shared_attributes[ $field ] = $value;
}
}
return $shared_attributes;
}
function mytheme_algolia_faceted_fields( $types = array() ){
if( $types && ! is_array( $types ) )
$types = array( $types );
$fields = array(
'mls' => 'string',
'price' => 'float',
'status' => 'string',
'listing_class' => 'string',
'acres' => 'float',
'number_bedrooms' => 'int',
'number_total_bathrooms' => 'float',
'address_street' => 'string',
'city' => 'string',
'zip' => 'string',
'county' => 'string',
'subdivision' => 'string',
'elementary_school' => 'string',
'intermediate_school' => 'string',
'middle_school' => 'string',
'high_school' => 'string',
'year_built' => 'int',
'latitude' => 'float',
'longitude' => 'float',
);
if( ! $types )
return $fields;
$return = array();
foreach( $fields as $field => $type ){
if( in_array( $type, $types ) ){
$return[ $field ] = $type;
}
}
return $return;
}
function mytheme_algolia_get_posts( $search ){
/*
'hits' => array [results]
'nbHits' => int 36947
'page' => int 0
'nbPages' => int 19
'hitsPerPage' => int 54
*/
$post_ids = wp_list_pluck( $search['hits'], 'post_id' );
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'posts_per_page' => $search['hitsPerPage'],
'post__in' => $post_ids
);
$query = new WP_Query( $args );
$query->found_posts = $search['nbHits'];
$query->max_num_pages = $search['nbPages'];
$query->paged = $search['page'];
return $query;
}
<?php
add_filter( 'algolia_post_listing_shared_attributes', 'mytheme_algolia_listing_custom_meta', 10, 2 );
function mytheme_algolia_listing_custom_meta( array $shared_attributes, WP_Post $post ) {
$remove_fields = array(
'comment_count',
'menu_order',
'post_author',
'post_type_label',
'images',
'post_mime_type',
'taxonomies',
'taxonomies_hierarchical',
'is_sticky',
'alt',
'metadata'
);
foreach( $remove_fields as $field ){
if( isset( $shared_attributes[ $field ] ) )
unset( $shared_attributes[ $field ] );
}
$listing = new MY_Listing( $post->ID );
if( ! $listing )
return $shared_attributes;
$searchable_fields = array(
'mls',
'price',
'status',
'listing_class',
'acres',
'number_bedrooms',
'number_total_bathrooms',
'address_street',
'city',
'zip',
'county',
'subdivision',
'elementary_school',
'intermediate_school',
'middle_school',
'high_school',
'year_built',
'latitude',
'longitude',
);
foreach( $searchable_fields as $field ){
$method = 'get_' . $field;
$meta_key = '_listing_' . $field;
if( $value = $listing->_get_field( $field ) ){ // this doesn't work :(
$shared_attributes[ $field ] = $value;
} elseif( $value = $listing->$method() ) { // this doesn't work :(
$shared_attributes[ $field ] = $value;
} elseif( $value = get_post_meta( $post->ID, $meta_key, true ) ) {
$shared_attributes[ $field ] = $value;
}
}
return $shared_attributes;
}
@rayrutjes
Copy link

In addition to my incoming support answer, here is how you could cast your attributes to integers:

if ( is_numeric( $value ) ) {
    $value = (float) $value;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment