so I've got a nice hierarchical system working involving custom post types and taxonomies. (see Custom post types, taxonomies, and permalinks for reference)
So if I have a post with mysite.com/taxonomy/subtaxonomy/tastes-good , it works, and I can access taxonomy pages in mysite.com/taxonomy/subtaxonomy etc etc
However, noticed a bug - I can put ANYTHING as part of the taxonomy part of the slug and it still goes to the post!
eg mysite.com/LAIOAJSDJ/AOIDJKOASJD/tastes-good... will still go to the post no problem, but I want it to 404!
What's the point of using this if the taxonomy term in the slug/url is irrelevant? I want it to only use a specific taxonomy term as part of the URL to go to the post... if the post is not matched to the taxonomy term it should 404!! Or at the very least, forward to the correct slug/url....
EDIT:
I tested out the ordinary post/category function by setting up a custom permalink structure of /%category%/%postname%/ ... This works well enough and forwards to the correct category, which is what I'm looking for in regards to above and suits functionality of my site
The code supplied below doesn't seem to work at all, is it related to how my rewrite rules are set up?
- my = custom post-type
mycategory = taxonomy
add_filter('rewrite_rules_array', 'mmp_rewrite_rules'); function mmp_rewrite_rules($rules) { $newRules = array(); $newRules['my/(.+)/page/?([0-9]{1,})/?$'] = 'index.php?mycategory=$matches[1]&paged=$matches[2]'; $newRules['my/(.+)/(.+)/(.+)/?$'] = 'index.php?my=$matches[3]'; $newRules['my/(.+)/?$'] = 'index.php?mycategory=$matches[1]'; return array_merge($newRules, $rules); }
code from reply
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 ); function wpd_single_cpt_queries( $query ){ if( $query->is_singular() && $query->is_main_query() && isset( $query->query_vars['mycategory'] ) ){ $tax_query = array( array( 'taxonomy' => 'mycategory', 'field' => 'slug', 'terms' => $query->query_vars['mycategory'], ) ); $query->set( 'tax_query', $tax_query ); } } add_action( 'pre_get_posts', 'wpd_single_cpt_queries' );
also used this to reference code (How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name)
I even used WP Permastructure plugin to see if that would solve the problem.. the plugin works fine but it's still the same issue - the taxonomy term itself is deemed irrelevant, it's just filler for the URL!
redirect_canonical()
.