• Hello all,

    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(
            '%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;
    }

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

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t know enough about rewrites to be able to spot any problems in code. In case no one else spots a problem either, the next step is to output the actual query string from the ‘posts_request’ filter. There should be some conflicting clauses which are causing no posts to be returned. What the conflicts are will be a clue to what part of your permastruct is not working right.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom permalink structure gives 40’ is closed to new replies.