/**
* VIP Legacy Related Posts (get post_id, title, url)
*
* Don't use for new projects, just use WPCOM_RelatedPosts directly, since it has hooks
* like jetpack_relatedposts_filter_args, jetpack_relatedposts_filter_filters
*
* For backwards compatability, this function finds related posts on the current blog
* using Elasticsearch, then converts the results to match the original sphere results format.
*
* @param int $max_num Optional. Maximum number of results you want (default: 5).
* @param array $additional_stopwords No longer used.
* @param bool $exclude_own_titles No longer used.
* @return array of related posts.
*/
function wpcom_vip_get_flaptor_related_posts($max_num = 5, $additional_stopwords = array(), $exclude_own_titles = true)
{
if (method_exists('Jetpack_RelatedPosts', 'init_raw')) {
$post_id = get_the_ID();
$related = Jetpack_RelatedPosts::init_raw()->set_query_name('MLT-VIP-Flaptor')->get_for_post_id($post_id, array('size' => $max_num));
$host = parse_url(home_url(), PHP_URL_HOST);
$source_info = array('sourcename' => get_bloginfo('name'), 'sourceurl' => home_url(), 'sourcetype' => 'same_domain');
if ($related) {
//rebuilding the array to match sphere related posts (and flaptor related posts)
$results = array();
foreach ($related as $result) {
$results[] = array('post_id' => $result['id'], 'url' => get_permalink($result['id']), 'title' => get_the_title($result['id']), 'timestamp' => get_the_time('Y-m-d', $result['id']), 'host' => $host, 'source' => $source_info);
}
return $results;
}
return false;
} else {
// Fallback for local environments where flaptor isn't available
$related_posts = array();
$host = parse_url(home_url(), PHP_URL_HOST);
$source_info = array('sourcename' => get_bloginfo('name'), 'sourceurl' => home_url(), 'sourcetype' => 'same_domain');
$post_id = get_the_ID();
$related_query_args = array('posts_per_page' => $max_num);
$categories = get_the_category($post_id);
if (!empty($categories)) {
$related_query_args['cat'] = $categories[0]->term_id;
}
$related_query = new WP_Query($related_query_args);
foreach ($related_query->get_posts() as $related_post) {
$related_post_id = $related_post->ID;
$related_posts[] = array('url' => get_permalink($related_post_id), 'title' => get_the_title($related_post_id), 'timestamp' => get_the_time('Y-m-d', $related_post_id), 'host' => $host, 'source' => $source_info, 'post_id' => $related_post_id);
}
return $related_posts;
}
}