• Hey there,
    I would first like to say thank you for the good work, I’m running my first project on wordpress and find your plugin both useful and easy to use 🙂

    Here’s what I’m trying to do:

    I have three CPT:

    – Companies
    – Company categories
    – Samples

    Company categories are CPTonomy for companies,
    Companies are CPTonomy for Samples

    I want to be able to use this URL:
    site/samples/{company-category-name}

    To reach an archive page of samples which displays only samples linked to companies linked to this particular company-category.

    Kind of using the taxonomy’s taxonomy of a custom post (things start to be complicated over here :D)

    Here is part of my function.php:

    add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
    
    function mmp_rewrite_rules($rules) {
    
        $newRules                               = array();
        $newRules['companies/(.+)/(.+?)$'] = 'index.php?companies=$matches[2]';
        $newRules['companies/(.+)/?$']          = 'index.php?post_type=companies&company_categories=$matches[1]&cpt_onomy_archive=1';
        $newRules['companies/?$']          = 'index.php?post_type=companies'; 
    
        $newRules['samples/(.+)/(.+?)$'] = 'index.php?samples=$matches[2]';
        $newRules['samples/(.+)/?$']          = 'index.php?post_type=samples&company_categories=$matches[1]&cpt_onomy_archive=1';
        $newRules['samples/?$']          = 'index.php?post_type=samples';
    
        return array_merge($newRules, $rules);
    }
    
    function filter_post_type_link($link, $post)
    {
        if ($post->post_type != 'companies' && $post->post_type != 'samples'){
            return $link;
        }
    
        if($post->post_type == 'samples'){
    	    if ($cats = get_the_terms($post->ID, 'companies')){
    	    	$cat = array_pop($cats);
    
    	    	if ($cats = get_the_terms($cat->term_id, 'company_categories')){
    	        	$link = str_replace('%company_categories%', array_pop($cats)->slug, $link);
    	    	}
    
    	    }
    	}
    
        if($post->post_type == 'companies'){
    	    if ($cats = get_the_terms($post->ID, 'company_categories')){
    	        $link = str_replace('%company_categories%', array_pop($cats)->slug, $link);
    	    }
    	}
    
        return $link;
    }
    
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);

    I tried to change the query_var of the companies CPT by “category_name” and to use this line:
    $newRules['samples/(.+)/?$'] = 'index.php?post_type=samples&companies=$matches[1]&cpt_onomy_archive=1';

    but I still got Page not found on site/samples/{company-category-name} AND also on company page (which I did not before). So the solution seems to be somewhere…

    Do you got some of your magic for me ?

    Thank you very much !

    https://ww.wp.xz.cn/plugins/cpt-onomies/

The topic ‘Two level CPT-onomy rewrite rule’ is closed to new replies.