Custom post type permalink broken : 3 match for a single custom post.
-
Hi All,
I submit you a problem that drives me mad… I created a custom post type called “product” with custom permalinks using this very good tutorial : https://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2. Everything was working well since… I flushed all the rewrite rules ! #BigFailHere is my code, and I will comment what I’ve done. I hope you will find what is wrong.
1) Registering of custom post type called “product”:
add_action('init', 'my_products_init'); function my_products_init() { $args = array( 'label' => __('My Product DB'), 'singular_label' => __('My Product DB'), 'public' => true, 'show_ui' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'publicly_queryable' => true, 'query_var' => true, 'rewrite' => false, 'supports' => array('title', 'editor', 'thumbnail', 'comments') ); register_post_type( 'product' , $args ); }
I created a lot of custom posts “products” so no problem at this stage.
2/ I registered a custom permalink structure for “product” post types, which is composed of 2 %tags% :
add_action('init', 'my_rewrite_init'); function my_rewrite_init() { global $wp_rewrite; $wp_rewrite->add_rewrite_tag("%product%", '([^/]+)', "product="); $wp_rewrite->add_rewrite_tag("%producttitle%", '([^/]+)', 'producttitle='); $products_structure = '/Product-DB/%producttitle%/%product%/The-Product'; $wp_rewrite->add_permastruct('product', $products_structure, false); }
At this stage, the link of each post is well replaced my the permalink structure “products_structure”. So no problem at this stage too.
3/ Substitution rules of %producttitle% tag with “_producttitle” custom field.
add_filter('post_type_link', 'producttitle_permalink', 10, 3); function producttitle_permalink($permalink, $post, $leavename) { $no_data = 'no-title'; $post_id = $post->ID; if($post->post_type != 'product' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) return $permalink; $var1 = get_post_meta($post_id, '_producttitle', true); $var1 = sanitize_title($var1); if(!$var1) { $var1 = $no_data; } $permalink = str_replace('%producttitle%', $var1, $permalink); return $permalink; }
Again, each %tag% is replaced.
A sample target link of my custom products post is https://www.mysite.com/my-wpmu-blogid/Product-DB/product-title-sample/s79925215/The-Product/ BUT when I load this link I got a 404 error. I don’t understand why because, again, EVERYTHING was working well since 1 week, till I decided to “flush rewrite rules”.
I installed Rewrite Rules Inspector and this plugin says 3 rules match my custom products URLs :
Product-DB/[^/]+/[^/]+/([^/]+)/?$ index.php?attachment=$matches[1] missing Product-DB/([^/]+)/([^/]+)/The-Product(/[0-9]+)?/?$ index.php?producttitle=$matches[1]&product=$matches[2]&page=$matches[3] missing (.?.+?)(/[0-9]+)?/?$ index.php?pagename=$matches[1]&page=$matches[2] page
I don’t know why the “page” post type is matched… perhaps this is the source of my problem. And if so, I don’t understand why it doesn’t work anymore as it worked yesterday.
Any idea ?
Best regards,
–Looic
- The topic ‘Custom post type permalink broken : 3 match for a single custom post.’ is closed to new replies.