1
votes

I'm hoping someone will be able to help me with a Wordpress URL rewrite...i've attempted a couple but have got nowhere near so far. To give you an overview of where I am at...

I first register my taxonomy

function curriculum_taxonomy() {
  register_taxonomy(
    'taxonomy_type',  
    'taxonomy',        
    array(
        'hierarchical' => true,
        'show_admin_column' => true,
        'label' => 'taxonomy type', 
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'taxonomy', 
            'with_front' => true,
            'hierarchical' => true
        )
    )
  );
}
add_action( 'init', 'curriculum_taxonomy');

registered taxonomy_type with a rewritten slug of taxonomy

so currently the URL is www.baseurl/taxonomy

then I register the post_type whose relevant args are

    $args = array(
    'capability_type' => 'post',
    'rewrite'         => array('slug' => 'taxonomy/%parent_term%','with_front' => false),
    'taxonomies'      => 'taxonomy_type',
    'query_var' => true,
    'can_export' => true,
    'publicly_queryable' => true,

  );

I then programatically swap out the %parent_term% with the term slug

function filter_taxonomy_link($link, $post) {
    if ($post->post_type != 'taxonomy') {
        return $link;
    }
    if ($cats = get_the_terms($post->ID, 'taxonomy_type')) {
      $linkSlug = array_pop($cats)->slug;
      $link = str_replace('%parent_term%', $linkSlug, $link);
      return $link;
    }
}
add_filter('post_type_link', 'filter_curriculum_link', 10, 2);

so my URL is currently www.baseurl/taxonomy/[parent-term]/ with taxonomy being static

This gives me 90% correct URL's

now here's where i'm running into problems...

this structure exists www.baseurl/taxonomy/[parent-term]/[post_name] (for example post_name might be an about page or some page relating to that parent term.

This structure also exists www.baseurl/taxonomy/[parent-term]/content which is a custom archive of all child-terms. One of these will exist per parent-term. content is a static slug.

What i'm needing is a rewrite of www.baseurl/taxonomy/[parent-term]/content/[child_post_name] only when [child_post_name] has a [child-term]. And also a rewrite of www.baseurl/taxonomy/[parent-term]/content/[child_post_name]/[grandchild_post_name] only when [grandchild_post_name] has a [grandchild-term].

So to rephrase that...

if a post has only one parent term, the url should be www.baseurl/taxonomy/[parent-term]/[post_name] which I already have working.

but if a post has a child term then it should be www.baseurl/taxonomy/[parent-term]/content/[child_post_name]

and lastly if a post has a grandchild term then it should be www.baseurl/taxonomy/[parent-term]/content/[child_post_name]/[grandchild_post_name]

i'm really not sure how to go about making that relationship between child_post_name and grandchild_post_name. One idea i've had was to make the child_post_name another custom post type and prepend a rewrite to it so it fits in that structure...but thats a LOT of work and even at least some kind of confirmation on that would make me feel better about it.

even a point in the right direction would be greatly appreciated as i'm pretty stumped by this.

thanks.

1

1 Answers

0
votes

I was able to answer my own question. I figured i'd answer this if anyone else had a similiar problem.

My issue was actually in the structure of my post type and its corresponding taxonomies. Rather than using children and grandchildren terms, I split those into their own parent term and when I registered my post type, I changed it's capability type to page.

Rather than making the terms children of each other I made the posts themselves hierarchical. This solved my URL issue and also made my content more organised as a result.

If anyone needs an example i'd be happy to post a code example up