如何捕捉重写规则,然后显示特定的帖子?

时间:2017-08-16 作者:Monkey Monk

我试图捕获重写规则发送的参数,然后显示选定的帖子。

但现在,即使我可以检索这些参数,页面也会重定向到我自定义帖子类型的存档。其中,我需要显示与来自重写规则给定的查询字符串的自定义查询构建相匹配的帖子。

add_action(\'init\', function () {
    $terms_slug = getCustomTermsSlug();
    // @note Catch /{foo}
    add_rewrite_rule(\'^(\' . implode(\'|\', $terms_slug) . \')/?\', \'index.php?post_type=custom&foo=$matches[1]\', \'top\');
});

add_action(\'init\', function () {
    add_rewrite_tag(\'%foo%\', \'([^&]+)\');
}, 10, 0);

add_action(\'pre_get_posts\', function ($query) {
    if (is_admin() || !$query->is_main_query()) {
        return;
    }

    if (get_query_var(\'foo\')) {
        $tax_query = [[
            \'taxonomy\' => \'custom\',
            \'field\' => \'slug\',
            // @note "foo" is a custom taxonomy term
            \'terms\' => get_query_var(\'foo\'),
        ]];

        $query->set(\'tax_query\', $tax_query);
    }

    return $query;
});
首先,我不明白为什么它会重定向到归档页面?

第二,也许“pre\\u get\\u posts”不是进行定制查询的正确位置,但我可以在哪里进行查询?

第三,如何显示所选帖子以响应给定的干净URL?

谢谢你的帮助!;-)

@EDITI尝试过使用“template\\u redirect”或“parse\\u request”之类的操作,但感觉真的很粗糙。有人知道如何强制重定向(例如重写匹配)到自定义查询的第一个帖子吗?

1 个回复
SO网友:Monkey Monk

正如@milo所说,正确的方法是使用“parse\\u request”操作,该操作位于“pre\\u get\\u posts”操作之前(请参阅https://codex.wordpress.org/Plugin_API/Action_Reference).

我提供的完整解决方案:

add_action(\'init\', function () {
    $refs_slug = getCustomRefTermsSlug(); // this will return an array of all the terms slug existing in the custom "ref" taxonomy

    // @note Catch /{foo|bar|baz} and redirect to index.php?catch_me=true?ref={foo|bar|baz}
    add_rewrite_rule(\'^(\' . implode(\'|\', $refs_slug) . \')/?\', \'index.php?post_type=custom&catch_me=true&ref=$matches[1]\', \'top\');
});

add_action(\'init\', function () {
    add_rewrite_tag(\'%ref%\', \'([^&]+)\');
    add_rewrite_tag(\'%catch_me%\', \'([^&]+)\');
}, 10, 0);

add_action(\'parse_request\', function ($query) {
    if (array_key_exists(\'catch_me\', $query->query_vars) && $query->query_vars[\'catch_me\']) {
        $args = [
            \'post_type\' => \'custom\',
            \'posts_per_page\' => 1,
            \'fields\' => \'ids\',
            \'tax_query\' => [],
        ];

        if (array_key_exists(\'ref\', $query->query_vars)) {
            $args[\'tax_query\'][] = [
                \'taxonomy\' => \'ref\',
                \'field\' => \'slug\',
                \'terms\' => $query->query_vars[\'ref\'],
            ];
        }

        // get the right post
        $tmp_query = new \\WP_Query($args);
        $post_id = $tmp_query->posts[0];

        // tell wordpress what post to display
        $query->query_vars = [
            \'post_type\' => \'custom\',
            \'p\' => $post_id,
        ];
    }

    return $query;
});

add_filter(\'post_type_link\', function ($url, $post) {
    if (get_post_type($post) == \'custom\') {
        $refs_slug = getCustomRefTermsSlug($post->ID); // this will return an array of all the terms slug of the custom "ref" taxonomy assigned to the post

        // @note Return "/{foo|bar|bar}" as the permalink of the post
        return \'/\' . implode(\'/\', $refs_slug);
    }

    return $url;
}, 10, 2);
请随时给我评论!;-)

结束

相关推荐