Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created May 5, 2020 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustardBees/23677e45ff5cae66c519a5065738fe7e to your computer and use it in GitHub Desktop.
Save mustardBees/23677e45ff5cae66c519a5065738fe7e to your computer and use it in GitHub Desktop.
WordPress WP Search with Algolia plugin - push custom fields to Algolia.
<?php
/**
* Push custom fields to Algolia.
*
* @param array $attributes
* @param WP_Post $post
*
* @return array
*/
function iweb_algolia_product_attributes( array $attributes, WP_Post $post ) {
// Bail if this isn't our product post type.
if ( 'product' !== $post->post_type ) {
return $attributes;
}
$sku = get_post_meta( $post->ID, '_cmb_sku', true );
$attributes['sku'] = $sku;
return $attributes;
}
add_filter( 'algolia_post_shared_attributes', 'iweb_algolia_product_attributes', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'iweb_algolia_product_attributes', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment