public function filter_posts_request($request, $query)
{
if (!ep_elasticpress_enabled($query) || apply_filters('ep_skip_query_integration', false, $query)) {
return $request;
}
$query_vars = $query->query_vars;
/**
* Allows us to filter in searchable post types if needed
*
* @since 2.1
*/
$query_vars['post_type'] = apply_filters('ep_query_post_type', $query_vars['post_type'], $query);
if ('any' === $query_vars['post_type']) {
unset($query_vars['post_type']);
}
$new_posts = apply_filters('ep_wp_query_search_cached_posts', array(), $query);
if (count($new_posts) < 1) {
$scope = 'current';
if (!empty($query_vars['sites'])) {
$scope = $query_vars['sites'];
}
$formatted_args = ep_format_args($query_vars);
/**
* Filter search scope
*
* @since 2.1
*
* @param mixed $scope The search scope. Accepts `all` (string), a single
* site id (int or string), or an array of site ids (array).
*/
$scope = apply_filters('ep_search_scope', $scope);
$ep_query = ep_query($formatted_args, $query->query_vars, $scope);
if (false === $ep_query) {
return $request;
}
$query->found_posts = $ep_query['found_posts'];
$query->max_num_pages = ceil($ep_query['found_posts'] / $query->get('posts_per_page'));
foreach ($ep_query['posts'] as $post_array) {
$post = new stdClass();
$post->ID = $post_array['post_id'];
$post->site_id = get_current_blog_id();
if (!empty($post_array['site_id'])) {
$post->site_id = $post_array['site_id'];
}
// ep_search_request_args
$post_return_args = apply_filters('ep_search_post_return_args', array('post_type', 'post_author', 'post_name', 'post_status', 'post_title', 'post_parent', 'post_content', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', 'post_mime_type', 'comment_count', 'comment_status', 'ping_status', 'menu_order', 'permalink', 'terms', 'post_meta'));
foreach ($post_return_args as $key) {
if ($key === 'post_author') {
$post->{$key} = $post_array[$key]['id'];
} elseif (isset($post_array[$key])) {
$post->{$key} = $post_array[$key];
}
}
$post->elasticsearch = true;
// Super useful for debugging
if ($post) {
$new_posts[] = $post;
}
}
do_action('ep_wp_query_non_cached_search', $new_posts, $ep_query, $query);
}
$this->posts_by_query[spl_object_hash($query)] = $new_posts;
do_action('ep_wp_query_search', $new_posts, $ep_query, $query);
global $wpdb;
return "SELECT * FROM {$wpdb->posts} WHERE 1=0";
}