Code-Snippets-Functions/Execute a function on a child site/Relevanssi/add-variation-product-name-in-search.txt

18 lines
581 B
Text

add_filter( 'relevanssi_content_to_index', 'rlv_index_variation_skus', 10, 2 );
function rlv_index_variation_skus( $content, $post ) {
if ( 'product' === $post->post_type ) {
$args = array(
'post_parent' => $post->ID,
'post_type' => 'product_variation',
'posts_per_page' => -1
);
$variations = get_posts( $args );
if ( ! empty( $variations ) ) {
foreach ( $variations as $variation ) {
$sku = get_post_meta( $variation->ID, '_sku', true );
$content .= " $sku {$variation->post_title}";
}
}
}
return $content;
}