WordPress.org

Ready to get started?Download WordPress

Forums

Hierarchical custom taxonomy permalink gives 404 (9 posts)

  1. Geerth
    Member
    Posted 2 years ago #

    Hi,

    I'm struggling with a strange problem. First the code:
    [Code moderated as per the Forum Rules. Please use the pastebin]

    with this code, when i visit http://www.domain.com/producten/term1/subterm/product this gives a 404. As soon as I delete 'hierarchical' => true, within the rewrite part of the custom taxonomy and I only use one term, it all works.

    So in short:
    http://www.domain.com/producten/term1/subterm/product <-- does not work
    http://www.domain.com/producten/term1/subterm/ <-- works
    http://www.domain.com/producten/term1/<-- works

    I've no idea what to do anymore, I've flushed the permalinks several times. I switched theme's during updates. I do have a feeling that I have to alter the .htacces file, but i don't really understand what to do. I've red a dozen of articles about this, but most problems are slightly different (or I dont understand it, because my english isn't that great).

    Does anyone have any suggestions?

  2. Geerth
    Member
    Posted 2 years ago #

    The code can be found here: http://pastebin.com/B1A7MvFx

  3. Geerth
    Member
    Posted 2 years ago #

    I still didn't fix this problem. Does anyone got a solution? I've tried working with plugins, but they didn't support the hierarchical part.

  4. flashpunk
    Member
    Posted 2 years ago #

    I'm looking for a solution to this as well.. Any luck?

  5. Jorge A. Gonzalez
    Member
    Posted 2 years ago #

    Interested in this as well.

    Looking to structure URLs like:
    http://www.domain.com/producten/term1/subterm/product

    Right now we are forced to use pages as the hierarchy, but looking to build out using custom taxonomies and custom post types.

    Any idea if this will be built out in the future?

  6. belg4mit
    Member
    Posted 2 years ago #

    *bump* just ran into this as well.

    Have a custom post type and a custom taxonomy,
    and after navigating the taxonomy archives you end up
    at a URL with the post-type as the base of the permalink
    rather than the custom taxonomy. It works, but is ugly.

    The problem seems to be that the slug parameter for
    rewrite option to register_post_type is undocumented/
    does not support normal permalink variables...

    If you don't need to support multiple taxonomies,
    including the WordPress native one for posts, it looks
    like this plugin might work: Taxonomic SEO permalinks/

  7. belg4mit
    Member
    Posted 2 years ago #

    After a few dead ends and incomplete solutions, I finally have something. My hierarchy is a custom taxonomy followed by the slug for a custom post type, which is different from the OP's query, but the terminating slug is all that really matters, and you can add additional depth/validation as needed.

    If you have a custom 'widget' type (with a rewrite slug of widget), and widgets are filed under 'color's, you can do the following:

    #Fix links for custom type widget to have colored permalink
    function custom_post_link($post_link, $id = 0){
      $post = get_post($id);
    
      if( is_object($post) &&
          $post->post_type == 'widget' &&
          $terms = wp_get_object_terms($post->ID, 'color') ){
          $color = 'color/'. $terms[0]->slug;
          #This turns the widget/slug link into color/$color/slug
          return preg_replace('/\bwidget\b/', $color, $post_link);
      }
    
      return $post_link;
    }
    add_filter('post_type_link', 'custom_post_link', 1, 3);
    
    #Create rule to check for posts in color, and pass the terminal slug to find the content
    #We discard the hierarchy information in the URL here, but you could pass it on if needed
    add_rewrite_tag("%color%", '([^/]+)');
    add_rewrite_rule('^color/[^/]+/([^/]*)/?','index.php?widget=$matches[1]','top');
    flush_rewrite_rules();
  8. globaltimoto
    Member
    Posted 1 year ago #

    I finally have something

    @belg4mit

    How are you getting on with the solution you posted here ? Are there any caveats worth mentioning ?

    How would you write the code to achieve:

    domain.com/custom_post_type/custom_tax_level_1/custom_tax_level_2/post

    or

    domain.com/custom_tax_level_1/custom_tax_level_2/custom_post_type/post

  9. belg4mit
    Member
    Posted 1 year ago #

    The solution is working well.

    One caveat is that the permalink (if shown) under the title in the editor for saved drafts and posts is mangled, and the slug is not editable here after the first save; you can still quick edit it in the post listing. But otherwise no problems, and that's minor enough (arguably cosmetic) to ignore in my case. This ought to be fixable, but I've not had the tuits to tackle it.

    As noted, only the terminal portion of the URI is important, which means the code ought to work for any depth. I vaguely recall having tested it with two levels and being fine.

Topic Closed

This topic has been closed to new replies.

About this Topic