{"id":4790268,"date":"2014-04-09T13:07:03","date_gmt":"2014-04-09T13:07:03","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/custom-permalink-structure-gives-40\/"},"modified":"2016-08-21T19:10:07","modified_gmt":"2016-08-21T19:10:07","slug":"custom-permalink-structure-gives-40","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/custom-permalink-structure-gives-40\/","title":{"rendered":"Custom permalink structure gives 40"},"content":{"rendered":"<p>Hello all,<\/p>\n<p>For a Custom Post Type called <code>matches<\/code> 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.<\/p>\n<p>I have tried removing the .htaccess, re-saving the permalinks, flushing the rewrites etc. None of it did any good.<\/p>\n<p>The code I use is as follows:<\/p>\n<pre><code>add_action( &#039;init&#039;, array( $this, &#039;match_permalink_structure&#039; ) );\nadd_filter( &#039;post_type_link&#039;, array( $this, &#039;better_match_permalinks&#039; ), 10, 3 );\n\nfunction match_permalink_structure() {\n\tglobal $wp_rewrite;\n\n\t$structure = trailingslashit( __( &#039;match&#039;, &#039;apollo&#039; ) );\n\t$structure .= trailingslashit( &#039;%year%\/%month%\/%day%&#039; );\n\t$structure .= trailingslashit( &#039;%matches%&#039; );\n\n\t$wp_rewrite-&gt;add_rewrite_tag( &quot;%matches%&quot;, &#039;([^\/]+)&#039;, &quot;matches=&quot; );\n\t$wp_rewrite-&gt;add_permastruct( &#039;matches&#039;, $structure, true );\n}\n\nfunction better_match_permalinks( $permalink, $post, $leavename ) {\n\t$rewritecode = array(\n        &#039;%year%&#039;,\n        &#039;%month%&#039;,\n        &#039;%day%&#039;,\n        $leavename ? &#039;&#039; : &#039;%matches%&#039;,\n    );\n\n\t$timestamp = get_field( &#039;match_datetime&#039;, $post-&gt;ID );\n\n\tif( ! empty( $timestamp ) ) {\n\t\t$year = date( &#039;Y&#039;, $timestamp );\n\t\t$month = date( &#039;m&#039;, $timestamp );\n\t\t$day = date( &#039;d&#039;, $timestamp );\n\t} else {\n\t\t$year = date( &#039;Y&#039;, strtotime( $post-&gt;post_date ) );\n\t\t$month = date( &#039;m&#039;, strtotime( $post-&gt;post_date ) );\n\t\t$day = date( &#039;d&#039;, strtotime( $post-&gt;post_date ) );\n\t}\n\n\t$rewrite_replace = array(\n\t\t$year,\n\t\t$month,\n\t\t$day,\n\t\t$post-&gt;post_name,\n\t);\n\n\t$permalink = str_replace( $rewritecode, $rewrite_replace, $permalink );\n\n\treturn $permalink;\n}<\/code><\/pre>\n<p>Does anyone have any idea what I could possibly be doing wrong, or what I need to do to fix this issue?<\/p>\n","protected":false},"template":"","class_list":["post-4790268","topic","type-topic","status-closed","hentry","topic-tag-permalink"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/4790268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/4790268\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=4790268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}