如何更改wordpress中的查询搜索

问题描述 投票:0回答:1

我有一个包含超过 200,000 篇文章的大型网站 在 WordPress 中搜索时,此搜索会在标题、内容和摘录中查找所需的单词 在这几篇文章中,慢查询的结果超过50秒,拖慢了MariaDB

我想编写一个过滤器来将此查询限制为仅标题

我认为这个缓慢的查询将会被修复,问题将会得到解决 请帮助编写此过滤器,或者如果您知道问题可以通过其他解决方案解决,请提供帮助 谢谢你

在文件中 wp-includes/class-wp-query.php

wordpress search filter query-optimization
1个回答
0
投票

您可以尝试一下这个代码并让我知道它是否有效吗?

function limit_search_to_titles($query) {
if ($query->is_search && !is_admin()) {
    $query->set('post_type', 'post'); // Adjust 'post' to match your custom post types if needed
    $query->set('posts_per_page', -1); // Include all results, you can limit this if necessary
    $query->set('s', ''); // Clear the search term
    $query->set('title', $query->query_vars['s']); // Set the title to the search term
} } add_action('pre_get_posts', 'limit_search_to_titles');
© www.soinside.com 2019 - 2024. All rights reserved.