I’ve been beating my head against the wall for a few days trying to figure this out. I have a custom post type and custom taxonomy and I’m trying to get my URL’s to work.
Here is the URL structure that I am going for:
http://url.dev/custom-post-type/taxonomy/post-name/
The first two parts are working. I can go to just custom-post-type
and it uses my archive-custom-post-type.php file. I can also go to /custom-post-type/taxonomy
and it uses my taxonomy-name.php.
However, when I try to go to an actual post, it gives me a 404 error page. I am using the Rewrite Analyzer plugin, and it looks like my rewrite rules are correct. Here are a few screenshots.
That looks right, that’s what needs to happen. But I get a 404 error when I try to go to one of the single products page.
Here is my actual rewrite rule in my functions.php
function taxonomy_slug_rewrite($wp_rewrite) {
$rules = array();
// get all custom taxonomies
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
// get all custom post types
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');
foreach ($post_types as $post_type) {
foreach ($taxonomies as $taxonomy) {
// go through all post types which this taxonomy is assigned to
foreach ($taxonomy->object_type as $object_type) {
// check if taxonomy is registered for this custom type
if ($object_type == $post_type->rewrite['slug']) {
// get category objects
$terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
// make rules
foreach ($terms as $term) {
$rules[$object_type . "https://wordpress.stackexchange.com/" . $term->slug . '/([^/]*)/?'] = 'index.php?pagename=$matches[1]&' . $term->taxonomy . '=' . $term->slug;
}
}
}
}
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');
Is there anything I’m doing wrong?
If I turn the permalinks back to the default, I can go to: http://uss.local/?ultrasound-equipment=sequoia-512&equipment-manufacturer=acuson and it works just fine. But when I switch over to “Post Name” permalink it doesn’t work.