/** * Delete post from aliyun after posts deleted. * * @param int $postId ID of the post which has been deleted. * * @return void */ public function afterDeletePost($postId) { $post = get_post($postId); if (!$post || !in_array($post->post_type, $this->acceptedSyncTypes) || $post->post_parent != 0) { return; } try { $this->getOpenSearchClient()->deletePosts(array($post)); } catch (AliyunOpenSearchException $e) { wp_die(sprintf('从阿里云删除文章时发生错误:%s,请检查您的配置是否有误。<a href="%s" target="_blank">查看错误码说明</a>', $e->getMessage(), AliyunOpenSearch::getErrorCodeReferencesUrl())); } }
/** * Filter `pre_get_posts`. * * @param WP_Query $wp_the_query * @return WP_Query */ public function preGetPosts($wp_the_query) { /** @var WP_Query $wp_the_query */ if ($wp_the_query->is_search() && !$wp_the_query->is_admin && $wp_the_query->is_main_query()) { $this->keyword = $wp_the_query->query['s']; $this->limit = isset($wp_the_query->query_vars['posts_per_page']) ? $wp_the_query->query_vars['posts_per_page'] : 10; if (isset($wp_the_query->query_vars['paged'])) { $this->paged = intval($wp_the_query->query_vars['paged']); $this->paged = $this->paged > 0 ? $this->paged : 1; } $offset = ($this->paged - 1) * $this->limit; try { $ret = $this->getOpenSearchClient()->search("default:'{$this->keyword}' AND post_status:'publish'", $offset, $this->limit); } catch (AliyunOpenSearchException $e) { wp_die(sprintf('搜索文章时发生错误:%s,请检查您的配置是否有误。<a href="%s" target="_blank">查看错误码说明</a>', $e->getMessage(), AliyunOpenSearch::getErrorCodeReferencesUrl())); } $this->posts = $ret['posts']; $this->postCount = $ret['total']; $this->pageCount = ceil($this->postCount / $this->limit); foreach ($this->posts as $post) { $this->ids[] = $post->ID; } $wp_the_query->query = array(); $wp_the_query->query_vars['post_in'] = $this->ids; $wp_the_query->query_vars['post_type'] = null; $wp_the_query->query_vars['s'] = null; $wp_the_query->query_vars['paged'] = null; // set_query_var('post__in', $this->ids); // set_query_var('post_type', null); // set_query_var('s', null); // set_query_var('paged', null); } return $wp_the_query; }