Created
November 23, 2020 04:00
-
-
Save MogulChris/e753bb4d76c873c20d12afea15f6b6aa to your computer and use it in GitHub Desktop.
ElasticPress modifications
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
//Search fields indexed by post type. Yes, I should do this in a tidier way. | |
global $search_index_fields; | |
$search_index_fields = array( | |
'still_image' => array('people','business','location','format_original','author','notes'), | |
'audio' => array('people','business','transcript','format_original','author','additional'), | |
'video' => array('people','business','format_original','transcript','author','additional'), | |
'person' => array('name','known_as','maiden_name','military_identification','birthplace','parents','partner','children','deathplace','biography'), | |
'text' => array('people','business','location','format_original','author','notes','publisher','transcript') | |
); | |
//This function returns our full list of meta fields | |
//It either returns a plain array of raw meta keys, or the same meta keys but formatted like meta.[key].value | |
function kb_get_search_fields($raw = false){ | |
global $search_index_fields; | |
$all_fields = ['post_title']; | |
foreach($search_index_fields as $post_type => $fields){ | |
foreach($fields as $field){ | |
$key = "meta.$field.value"; | |
if(!in_array($key,$all_fields)) $all_fields[] = ($raw ? $field : $key); | |
} | |
} | |
return $all_fields; | |
} | |
//Add fields to the plugin UI | |
function knowledgebank_add_custom_field_elasticpress_weighting( $fields, $post_type ) { | |
if(in_array($post_type,['still_image','audio','video','person','text'])){ | |
$fields['meta'] = array( | |
'label' => 'Custom Fields', | |
'children' => array(), | |
); | |
global $search_index_fields; | |
foreach($search_index_fields[$post_type] as $field){ // | |
// Change my_custom_field here to what you need. | |
$key = 'meta.' . $field; | |
$fields['meta']['children'][ $key ] = array( | |
'key' => $key, | |
'label' => ucwords($field), | |
); | |
} | |
} | |
return $fields; | |
} | |
add_filter('ep_weighting_fields_for_post_type','knowledgebank_add_custom_field_elasticpress_weighting', 10, 2); | |
//Prepare fancier meta | |
function knowledgebank_ep_prepare_meta_data($meta,$post){ | |
global $search_index_fields; | |
$search_fields = kb_get_search_fields(true); | |
foreach($meta as $key => $value){ | |
if(!in_array($key, $search_fields)) unset($meta[$key]); | |
} | |
//$transcript = get_field('transcript', $post->ID); | |
//$meta['transcript'] = array($transcript); | |
//name fields are repeaters, each row having first, middle, surname and (possibly) a reference field to a person record | |
foreach(['people','author','partner','children','parents'] as $name_field){ | |
$people = get_field($name_field, $post->ID); | |
if(!empty($people)){ | |
//get an array of names - we can easily do this by stripping the reference field on each row and imploding the remaining name fields | |
$names = array_map(function($person){ | |
if(!empty($person['record'])) unset($person['record']); | |
return implode(' ',$person); | |
},$people); | |
if(!empty($names)) $meta[$name_field] = implode(', ',$names); //eg John Smith, Rosie Jones, Derek Green | |
} | |
} | |
// print_r(array_keys($meta)); | |
// global $wpdb; | |
// $wpdb->query('DELETE FROM wp_options WHERE option_name LIKE "%wpcli_sync%"'); | |
// exit; | |
return $meta; | |
} | |
add_filter( 'ep_prepare_meta_data', 'knowledgebank_ep_prepare_meta_data',1,2); | |
//Search everything we index | |
function knowledgebank_ep_search_fields($search_fields, $args){ | |
return kb_get_search_fields(); | |
} | |
add_filter( 'ep_search_fields', 'knowledgebank_ep_search_fields',10,2); | |
//Which post types you want to index - presumably overrides the UI options... not 100% sure if needed | |
function knowledgebank_ep_indexable_post_types($types){ | |
global $search_index_fields; | |
return array_keys($search_index_fields); | |
} | |
add_filter('ep_indexable_post_types','knowledgebank_ep_indexable_post_types'); | |
//The one hook to rule them all - fully modify the query data which is to be posted to the server as JSON (still PHP at this stage) | |
function knowledgebank_ep_formatted_args( $formatted_args, $args ) { | |
if ( | |
! empty( $formatted_args['query']['bool']['should'] ) && | |
is_array( $formatted_args['query']['bool']['should'] ) | |
) { | |
global $search_index_fields; | |
$all_fields = kb_get_search_fields(); | |
$simple_query = array( | |
'simple_query_string' => array( | |
'query' => $formatted_args['query']['bool']['should'][0]['multi_match']['query'], | |
'fields' => $all_fields, | |
'default_operator' => 'AND', | |
'flags' => 'ALL' | |
) | |
); | |
$formatted_args['query']['bool']['should'] = array($simple_query); | |
} | |
return $formatted_args; | |
} | |
add_filter( 'ep_formatted_args', 'knowledgebank_ep_formatted_args', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment