$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Rewrite rule not working, suspect redirect string|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Rewrite rule not working, suspect redirect string

matteradmin7PV0评论

These rewrite rules are killing me. It is probable something little, but I can't find it.

I have a custom post type called 'tournament', it's permalink structure looks like

<domain>/<season>/<tournament-name>

For this custom post type everything works fine.

Now I have created a second custom post type 'match'. I set hierarchy to false. I added a meta-box to match where I can select the tournament and store it as post_parent.

Now I want its permalink structure to look like:

<domain>/<season>/<tournament-name>/match/<match-name>

I use the post_type_link hook to add a filter that creates the permalink I want and the init hook to add the rewrite rule. The permalink is created as expected, but when using the rewrite rule, I keep getting 404's.

I flushed the rewrite rules both in code and manually.
I checked the regex and it's valid.
I suspect it is the redirect string, but I'm not sure.
Any suggestions or ideas?

function rat_match_post_type_link( $link, $post, $leavename, $sample ){

    if ( $post->post_type == 'match' ){

        $parent = get_post($post->post_parent);

        $season = get_post_meta( $parent->ID, 'rat-tournament-season', true);

        error_log (home_url( $season . '/' . $parent->post_name . '/match/' . $post->post_name));

        return home_url( $season . '/' . $parent->post_name . '/match/' . $post->post_name);
    } else {
        return $link;
    }
}
add_filter('post_type_link', 'rat_match_post_type_link', 10, 4);

function rat_match_rewrite_rules()
{
    add_rewrite_rule('[0-9]{4}/(.+?)/match/([^/]+)?$','index.php?match=$matches[2]','top');
}
add_action('init', 'rat_match_rewrite_rules');

Note: I know I need to fix the post type link function in case there is no parent.

EDIT (8-feb-2019)

While trying to investigate further I observed the following:

When I access:

/index.php?match=test-match

The result is:

/2019/the-hague-4-star/match/test-match?match=test-match

And the latter results in a 404.

Post a comment

comment list (0)

  1. No comments so far