【发布时间】:2017-05-17 16:14:43
【问题描述】:
我的网站上的“目录”页面有一个自定义重写规则,以显示指定目录中的项目。一切都很好,除了如果用户尝试导航到大于第 9 页的页面,分页会神秘地返回 404 错误页面。下面是整个重写代码。网站上的其他档案对超过第 9 页的页面没有问题,所以我觉得这一定与我重写规则中的正则表达式有关,但我不知道它可能是什么。
function x_rewrite_rule_form_submissions()
{
add_rewrite_tag('%catalog%', '([^&]+)');
add_rewrite_rule('catalog/([^/]+)/page/([0-9]+)/?', 'index.php?catalog=$matches[1]&paged=$matches[2]', 'top' );
add_rewrite_rule('catalog/([^/]+)/?', 'index.php?catalog=$matches[1]', 'top');
}
add_action('init', 'x_rewrite_rule_form_submissions', 10, 0);
更新:使用调试这个插件,我发现 WP_Query 正在尝试使用 URL 中的分页信息在 post_type="post" 上运行主查询。我的理论是,正是这个查询导致 Wordpress 返回 404 错误消息。如何覆盖/禁用此主查询?
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 100, 10
【问题讨论】:
-
当您有 404 时,实际请求的 URL 是什么? (重写的)
-
作为一个快速更新,我认为这个问题与 Wordpress 试图在帖子查询中使用分页号但帖子不超过 9 页这一事实有关。所以也许我需要弄清楚如何阻止 Wordpress 在 /catalog 页面上运行其正常查询?
-
这是原网址,改写后的网址应以
index.php?catalog=...开头。当您有 404 时,您可以在控制台中读取请求的 url -
抱歉,这里是重写的:index.php?catalog=example-catalog-entry-name&paged=11