/** * Filter query string used for get_posts(). Search for posts and save for later. * Return a query that will return nothing. * * @param string $request * @param object $query * @since 0.9 * @return string */ 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; if ('any' === $query_vars['post_type']) { if ($query->is_search()) { /* * This is a search query * To follow WordPress conventions, * make sure we only search 'searchable' post types */ $searchable_post_types = get_post_types(array('exclude_from_search' => false)); // If we have no searchable post types, there's no point going any further if (empty($searchable_post_types)) { // Have to return something or it improperly calculates the found_posts return "WHERE 0 = 1"; } // Conform the post types array to an acceptable format for ES $post_types = array(); foreach ($searchable_post_types as $type) { $post_types[] = $type; } // These are now the only post types we will search $query_vars['post_type'] = $post_types; } else { /* * This is not a search query * so unset the post_type query var */ unset($query_vars['post_type']); } } $new_posts = array(); $new_posts = apply_filters('ep_pre_wp_query_search', $new_posts, $query); if (count($new_posts) < 1) { $scope = 'current'; if (!empty($query_vars['sites'])) { $scope = $query_vars['sites']; } $formatted_args = ep_format_args($query_vars); $search = ep_search($formatted_args, $scope); if (false === $search) { return $request; } $query->found_posts = $search['found_posts']; $query->max_num_pages = ceil($search['found_posts'] / $query->get('posts_per_page')); foreach ($search['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']; } $post->post_type = $post_array['post_type']; $post->post_name = $post_array['post_name']; $post->post_status = $post_array['post_status']; $post->post_title = $post_array['post_title']; $post->post_parent = $post_array['post_parent']; $post->post_content = $post_array['post_content']; $post->post_date = $post_array['post_date']; $post->post_date_gmt = $post_array['post_date_gmt']; $post->post_modified = $post_array['post_modified']; $post->post_modified_gmt = $post_array['post_modified_gmt']; $post->elasticsearch = true; // Super useful for debugging // Run through get_post() to add all expected properties (even if they're empty) $post = get_post($post); if ($post) { $new_posts[] = $post; } } do_action('ep_wp_query_search', $new_posts, $search, $query); } $this->posts_by_query[spl_object_hash($query)] = $new_posts; global $wpdb; return "SELECT * FROM {$wpdb->posts} WHERE 1=0"; }
/** * Filter query string used for get_posts(). Query for posts and save for later. * Return a query that will return nothing. * * @param string $request * @param object $query * @since 0.9 * @return string */ 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"; }