Support » Plugin: Custom Post Type UI » Taxonomy rewrite same as custom post type slug

  • Resolved dmjoyce92

    (@dmjoyce92)


    Archive page (works):
    http://www.pomonasupply.com/products/bridges-and-structures/

    Single Post (does not work):
    http://www.pomonasupply.com/products/bridges-and-structures/aluminum-structures/

    Used rewrite to match taxonomy slug to custom post type, but this breaks the single post view. Removing the taxonomy rewrite restores the single post view, but moves the archive url back to its original slug.

    Here is my setup:
    1. Taxonomy
    a) Name: Product Categories
    b) slug: product_category
    c) slug rewrite: products

    2. Custom Post Type
    a) Name: Products
    b) slug: product
    c) slug rewrite: products
    d) add taxonomy to slug (Custom Post Types Permalinks plugin): /products/%product_category%/%postname%/

    The conflict seems to be caused by part c. Is this just a configuration issue on my end, or does the plugin not currently support what I’m trying to do here? Any insight is much appreciated.

    Thanks

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    Good day, it’s not that CPTUI doesn’t support this type of thing, to certain degrees, because as you can see, you were able to configure things to achieve this setup. What isn’t supporting this is WordPress and the request parsing.

    With that permalink structure, it’s going to treat every url like this as if it’s a hierarchical taxonomy. Each post name is going to be assumed to be a child term of bridges-and-structures in your example, and so on.

    Assumed to be another term archive, not a post.
    http://www.pomonasupply.com/products/bridges-and-structures/aluminum-structures/

    Quite often, it’s best not to have these types of details match, for these reasons. Perhaps have the category slug be rewritten to “product-category” instead, for SEO reasons and such, while still enabling enough difference to not have issue with querying data.

    Thanks for the reply. I found a way to make the taxonomy rewrite work without killing the single post view: implemented the taxonomy rewrite as a separate mu-plugin instead of doing it through CPTUI. Not sure if it’s the order of the rewrites – between the taxonomy and the custom post types – that was causing the conflict, or what. For other people who face this issue, here is the code I used (pasted into a separate file called taxonomy-rewrites.php, and uploaded to mu-plugins directory):

     function rewrite_product_category() {
         // get the arguments of the already-registered taxonomy
         $product_category_args = get_taxonomy( 'product_category' ); // returns an object
    
         // make changes to the args
         // in this example there are three changes
         // again, note that it's an object
         $product_category_args->show_admin_column = true;
         $product_category_args->rewrite['slug'] = 'products';
         $product_category_args->rewrite['with_front'] = false;
    
         // re-register the taxonomy
         register_taxonomy( 'product_category', 'people', (array) $product_category_args );
     }
     // hook it up to 11 so that it overrides the original register_taxonomy function
     add_action( 'init', 'rewrite_product_category', 11 );

    credit to this stackexchange answer:
    https://wordpress.stackexchange.com/questions/161788/how-to-modify-a-taxonomy-thats-already-registered

    Plugin Author Michael Beckwith

    (@tw2113)

    Whatever works in my books.

    Some thoughts. I believe you will want to have changed that “people” to “product” from the register_taxonomy() line at the end for your case, as that’s the post type it’s supposed to be assigned to. If you didn’t change this part, I’m wondering if that’s part of why it seems to be working now.

    Otherwise, registering with the same taxonomy slug, product_category in this case, would overwrite the originally registered item, if I’m not mistaken, with the new arguments.

    Ah, good catch. I changed ‘people’ to ‘product’ and it seems to still work, so lucky for me I guess. Here is the revised code snippet for anyone else looking to reference:

    <?php
    /*
     * Replace Taxonomy slug with Post Type slug in url
     */
     function rewrite_product_category() {
         // get the arguments of the already-registered taxonomy
         $product_category_args = get_taxonomy( 'product_category' ); // returns an object
    
         // make changes to the args
         // in this example there are three changes
         // again, note that it's an object
         $product_category_args->show_admin_column = true;
         $product_category_args->rewrite['slug'] = 'products';
         $product_category_args->rewrite['with_front'] = false;
    
         // re-register the taxonomy
         register_taxonomy( 'product_category', 'product', (array) $product_category_args );
     }
     // hook it up to 11 so that it overrides the original register_taxonomy function
     add_action( 'init', 'rewrite_product_category', 11 );
    

    Thanks again for your assistance

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.