Skip to content

Instantly share code, notes, and snippets.

@thomascharbit
Created July 7, 2015 08:55
Show Gist options
  • Save thomascharbit/f99bfc9c4180f66df841 to your computer and use it in GitHub Desktop.
Save thomascharbit/f99bfc9c4180f66df841 to your computer and use it in GitHub Desktop.
Find related posts with Elasticpress
<?php
add_filter('ep_formatted_args', 'my_ep_formatted_args', 10, 2 );
function my_ep_formatted_args( $formatted_args, $args ) {
// Handle more like this queries. WP_QUery arg formatting is: array( 'more_like' => $post_id )
if ( ! empty( $args['more_like'] ) ) {
$formatted_args['query'] = array(
'more_like_this' => array(
'ids' => is_array($args['more_like']) ? $args['more_like'] : array( $args['more_like'] ),
// Add any field to search in
'fields' => array('post_title', 'post_content', 'terms.post_tag.name'),
// You can tweak elasticsearch parameters for better results
'min_term_freq' => 1,
'max_query_terms' => 12,
'min_doc_freq' => 1
)
);
}
return $formatted_args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment