I have a unique issue with URL structures on a custom post type.
I'm using CPTUI plugin to create a "product" post type. This works well enough on it's own. But in order to follow site structure, I have the "Custom Rewrite Slug" set to products/%product_category%
I then use the following code to rewrite the URL:
function add_custom_rewrite_rule($post_link, $id = 0) {
$post = get_post($id);
if (is_object($post)) {
$terms = wp_get_object_terms($post->ID, 'product_category');
if ($terms) {
return str_replace( '%product_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter('post_type_link', 'add_custom_rewrite_rule', 1, 3);
Again, this all works. I should mention that both "/products" and "/products/{product_category}" are both pages. These pages work as intended.
The issue is that the individual product pages are showing as 404 on the correct URL and I'm fully out of ideas on why or how to fix.