Custom permalink structure gives 404

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.

Read More

The code I use is as follows:

<?php
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(
        '%year%',
        '%month%',
        '%day%',
        $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(
        $year,
        $month,
        $day,
        $post->post_name,
    );

    $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?

Related posts

Leave a Reply

1 comment