• Resolved danielwerner23

    (@danielwerner23)


    Hi there,

    i have created a custom post type, named “products” and a taxonomy named “products_category”:

    add_action('init', function () {
    	register_taxonomy(
    		'products_category',
    		'products',
    		array(
    			'labels' => array(...),
    			'hierarchical' => true,
    			'query_var' => true,
    			'rewrite' => array(
    				'slug' => 'products/category',
    				'with_front' => false
    			)
    		)
    	);
    
    	register_post_type('products', array(
    		'labels' => array(....),
    		'public' 				=> true,
    		'publicly_queryable'  	=> true,
    		'query_var' 		=> true,
    		'rewrite' 			  	=> true,
    		'hierarchical' 		=> true,
    		'has_archive' 		=> true,
    		'show_in_rest'		=> true,
    		'exclude_from_search' 	=> false,
    		'supports'				=> array('title'),
    		'rewrite' 				=> array('slug' => 'products'),
    	));
    });

    The current url structure is:

    • products (archive/list of products -> use the archive-products.php template)
    • products/name_of_product (single view of a product -> use the single-products.php template)
    • products/category/name_of_category (list of products assigned to the category -> use the taxonomy-products_category.php template)

    This works fine and out of the box. My question is: Can i restructure this to “products/name_of_category” + “products/name_of_product”. My idea is to check if the term is in array like the pseudo code below and return the products assigned to the category if a category is given in the url, otherwise i return the normal single product.

    $categories = get_terms('products_category', array('hide_empty' => false, 'fields' => 'slugs'));
    
    in_array(get_query_var('products-category'), $categories) ...

    Thanks for the help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s not a good idea to make the URL element that WP would query for ambiguous. With your URL scheme, WP would first query for a matching term, then if not found, try querying for a matching product. It’s doable, but not very efficient. It’d make for a noticeable slow down on large DBs.

    You’d also need to verify new terms or products do not match any others in both schemas. With an unambiguous schema, it’s OK for terms to have the same name as products.

    There are multiple ways to accomplish this if you still want to proceed. I’d be inclined to build all the logic into a custom page template and have the associated rewrite rule send all /products/ requests to this page.

    Thread Starter danielwerner23

    (@danielwerner23)

    Thanks. I’ve “solved” the problem and got two options:

    1. As you said. I created a route /products and build a logic to return my templates. This is possible but plugins like yoast (breadcrump) and many others are not compatible to this and i have to rebuild many modules. All in all, its a lot of work and many errors are possible. So thats not my favorite solution.
    2. I have returned to the build in and out of the box working solution with the wordpress side templates (single, archive, taxonomy, …) and i accepted the structure: “products” / “products/name_of_product” / “products/category/name_of_category”.

    So thanks.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Just about anything is possible with customization, but some things aren’t worth the effort involved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘CPT taxonomy url structure’ is closed to new replies.