Skip to content

Instantly share code, notes, and snippets.

@wpbullet
Created February 20, 2018 20:27
Show Gist options
  • Save wpbullet/79739ade9ce975905aa33673cb81ef09 to your computer and use it in GitHub Desktop.
Save wpbullet/79739ade9ce975905aa33673cb81ef09 to your computer and use it in GitHub Desktop.
function SetSearchArgs( $formattedArgs, $args = [] ) {
if( isset( $args[ 'post_type' ] ) && ( count( $args[ 'post_type' ] ) > 1 || gettype( $args[ 'post_type' ] ) === 'string' ) ) {
if( gettype( $args[ 'post_type' ] ) === 'array' ) {
$existing_query = $formattedArgs[ 'query' ];
unset( $formattedArgs[ 'query' ] );
$formattedArgs[ 'query' ][ 'function_score' ][ 'query' ] = $existing_query;
foreach( $args[ 'post_type' ] as $postType ) {
$formattedArgs[ 'query' ][ 'function_score' ][ 'functions' ][] = [
'filter' => [
'multi_match' => [
'query' => $postType,
'fields' => [
"post_type.raw",
],
],
],
'weight' => (int) [ INT ],
];
}
}
}
$formattedArgs[ 'query' ][ 'function_score' ][ 'score_mode' ] = 'sum';
$formattedArgs[ 'query' ][ 'function_score' ][ 'boost_mode' ] = "multiply";
return $formattedArgs;
}
add_filter( 'ep_formatted_args', 'SetSearchArgs', 300, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment