Last active
September 27, 2022 13:26
-
-
Save tott/c0d70923c89ad54d27ce28efd58a56b8 to your computer and use it in GitHub Desktop.
ElasticPress group meta fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( | |
'ep_prepare_meta_data', | |
function ( $meta ) { | |
$meta_groups = [ | |
'group1' => [ | |
'_sku', | |
'_sale_price_dates_from', | |
'_sale_price_dates_to', | |
], | |
'group2' => [ | |
'total_sales', | |
'_tax_status', | |
], | |
]; | |
$_meta = []; | |
foreach ( $meta_groups as $group => $meta_keys ) { | |
foreach ( $meta_keys as $meta_key ) { | |
if ( isset( $meta[ $meta_key ] ) && ! empty( $meta[ $meta_key ][0] ) ) { | |
$_meta[ $group ][] = implode( ' ', (array) $meta[ $meta_key ] ); | |
unset( $meta[ $meta_key ] ); | |
} | |
} | |
} | |
foreach ( $_meta as $group => $grouped_meta_fields ) { | |
$meta[ $group ] = implode( ' ', array_unique( $grouped_meta_fields ) ); | |
} | |
return $meta; | |
} | |
); | |
add_filter( | |
'ep_weighting_configuration_for_search', | |
function ( $weight_config ) { | |
$group_weights = [ | |
'post' => [ | |
'group1' => 1, | |
'group2' => 1, | |
], | |
'page' => [], | |
'product' => [ | |
'group1' => 10, | |
'group2' => 10, | |
], | |
]; | |
foreach ( $group_weights as $post_type => $groups ) { | |
if ( isset( $weight_config[ $post_type ] ) ) { | |
foreach ( $groups as $group => $weight ) { | |
$weight_config[ $post_type ][ 'terms.' . $group . '.name' ] = [ | |
'weight' => $weight, | |
'enabled' => 1, | |
]; | |
} | |
} | |
} | |
return $weight_config; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment