For a Custom Post Type called 'matches' I created a custom permalink structure. The structure shows up just the way I want it, however, I get a 404 everytime I visit the post.

I have tried removing the .htaccess, re-saving the permalinks, flushing the rewrites etc. None of it did any good.

The code I use is as follows:

add_action( 'init', array( $this, 'match_permalink_structure' ) );
add_filter( 'post_type_link', array( $this, 'better_match_permalinks' ), 10, 3 );

function match_permalink_structure() {
    global $wp_rewrite;

    $structure = trailingslashit( __( 'match', 'apollo' ) );
    $structure .= trailingslashit( '%year%/%month%/%day%' );
    $structure .= trailingslashit( '%matches%' );

    $wp_rewrite->add_rewrite_tag( "%matches%", '([^/]+)', "matches=" );
    $wp_rewrite->add_permastruct( 'matches', $structure, true );

function better_match_permalinks( $permalink, $post, $leavename ) {
    $rewritecode = array(
        $leavename ? '' : '%matches%',

    $timestamp = get_field( 'match_datetime', $post->ID );

    if( ! empty( $timestamp ) ) {
        $year = date( 'Y', $timestamp );
        $month = date( 'm', $timestamp );
        $day = date( 'd', $timestamp );
    } else {
        $year = date( 'Y', strtotime( $post->post_date ) );
        $month = date( 'm', strtotime( $post->post_date ) );
        $day = date( 'd', strtotime( $post->post_date ) );

    $rewrite_replace = array(

    $permalink = str_replace( $rewritecode, $rewrite_replace, $permalink );

    return $permalink;

I have installed Rewrite Rules Inspector and that gives me the following for matches:

wedstrijd/[0-9]{4}/%month%/[0-9]{1,2}/[^/]+/attachment/([^/]+)/?$ index.php?attachment=$matches1 matches wedstrijd/[0-9]{4}/%month%/[0-9]{1,2}/[^/]+/attachment/([^/]+)/trackback/?$ index.php?attachment=$matches1&tb=1 matches wedstrijd/[0-9]{4}/%month%/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?attachment=$matches1&feed=$matches[2] matches wedstrijd/[0-9]{4}/%month%/[0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ index.php?attachment=$matches1&feed=$matches[2] matches wedstrijd/[0-9]{4}/%month%/[0-9]{1,2}/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$ index.php?attachment=$matches1&cpage=$matches[2] matches

I think that means that something is going wrong with %month% maybe? I am not sure.

Does anyone have any idea what I could possibly be doing wrong, or what I need to do to fix this issue?

1 Answer 1


Yes, the correct tag is %monthnum%. Here's a list of all the default rewrite tags, for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.