0

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!

  • WP will try to "salvage" even more extreme cases sometimes. There is a lot of related handling in redirect_canonical(). – Rarst Jan 6 '15 at 19:25
  • this seems to be a good basis for a solution, though i'm not sure how i'm going to make it redirect both 'parent' and 'child' taxonomy term when currently i can only get it to recognise the child taxonomy term in the permalink for wp_query (due to the rewrite rules)... – blahdyeck Jan 7 '15 at 11:47
1

I wouldn't call it a bug, you haven't done anything to place that restriction on the query for your post. It also seems like a fairly unlikely case where random strings will end up in place of your taxonomy terms for this to even present itself.

Anyway, the answer is to modify the query via pre_get_posts to add in whatever taxonomy data you need. I don't know your specifics, so this isn't a copy-paste solution, but it shows the general concept of what you need to do.

function wpd_single_cpt_queries( $query ){
    if( $query->is_singular()
        && $query->is_main_query()
        && isset( $query->query_vars['your_tax'] ) ){
            $tax_query = array(
                array(
                    'taxonomy' => 'your_tax',
                    'field' => 'slug',
                    'terms' => $query->query_vars['your_tax'],
                )
            );
            $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'wpd_single_cpt_queries' );
  • Doesn't seem to work, I think it's related to how I've set up the rewrite rules... see edit above – blahdyeck Jan 7 '15 at 10:14
  • You don't pass the taxonomy slug in your single post rewrite rule, so the condition you test for in pre_get_posts will never be met. – Milo Jan 7 '15 at 14:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.