Need Help with RewriteRules for CPT Taxonomies

I have built this structure using Pages in WP. Each page will be content-managed using fields that I create in ACF. That is no problem.

My client wants to be able to create various types of posts, News, Case Studies, Articles, and assign them to the various top-level sections, and also the About section, Projects etc.

So my strategy was to have the custom posts 'tagged' with a taxonomy for the top-level (Services) and another taxonomy for the Project. And then create listing pages for the posts on the correct page using ACF. This all works fine.

The problem comes when trying to assign the correct URL to the posts according to which taxonomies it has been tagged with.

If I can demonstrate with some examples URLs:

I want a News post tagged with 'service1' and 'project1' to have the url:

/service1/project1/news/news-title

and I've registered the Custom Post Type with a rewrite url to achieve that.

But I want to know how (or if) it's possible to also rewrite these URLs to support the other taxonomy terms:

/service2/project1/news-item-2
/service3/project3/news-item-3

Ultimately I want to maintain the URL structure as I drill down through the site:
So to clarify, the structure is built with nested Pages, and when I hit a Post I want to still be in that URL structure.

  • Majid

    Hello Ben
    I hope you are doing great today :slight_smile:

    We forwarded this ticket to our Second Level Support team to get their valuable feedback on this issue. One of our SLS members will reply back here as soon as more information is available.

    Please keep in mind that our SLS Team deals with more complicated issues, thus it may take a little longer for them to reply here. Thank you for your patience while we look into this further.

    Thanks,
    Majid

  • Ivan

    Hi Ben !
    It seems this example will be very helpful for you on it. You could try adding that snippet in a MU plugin ( more info about MU plugins is here )
    Also, I a little bit modified it for using two taxonomies together

    <?php
    add_action( 'init', 'register_my_types' );
    function register_my_types() {
    	register_post_type( 'recipes',
    		array(
    			'labels' => array(
    				'name' => __( 'Recipes' ),
    				'singular_name' => __( 'Recipee' )
    			),
    			'public' => true,
    			'has_archive' => true,
    		)
    	);
    	register_taxonomy( 'occasion', array( 'recipes' ), array(
    			'hierarchical' => true,
    			'label' => 'Occasions'
    		)
    	);
    	register_taxonomy( 'occasion2', array( 'recipes' ), array(
    			'hierarchical' => true,
    			'label' => 'Occasions'
    		)
    	);
    }
    // Add our custom permastructures for custom taxonomy and post
    add_action( 'wp_loaded', 'add_clinic_permastructure' );
    function add_clinic_permastructure() {
    	global $wp_rewrite;
    	add_permastruct( 'occasion', '%occasion%', false );
    	add_permastruct( 'occasion2', '%occasion2%', false );
    	add_permastruct( 'recipes', '%occasion%/%occasion2%/%recipes%', false );
    }
    // Make sure that all links on the site, include the related texonomy terms
    add_filter( 'post_type_link', 'recipe_permalinks', 10, 2 );
    function recipe_permalinks( $permalink, $post ) {
    	if ( $post->post_type !== 'recipes' )
    		return $permalink;
    	$terms = get_the_terms( $post->ID, 'occasion' );
    
    	$post_terms = array();
    	if ( $terms ) {
    		foreach ( $terms as $term )
    			$post_terms[] = $term->slug;
    	}
    	$terms2 = get_the_terms( $post->ID, 'occasion2' );
    
    	$post_terms2 = array();
    	if ( $terms2 ) {
    		foreach ( $terms2 as $term2 )
    			$post_terms2[] = $term2->slug;
    	}
    	return str_replace( array( '%occasion%', '%occasion2%' ), array( implode( ',', $post_terms ), implode( ',', $post_terms2 ) ) , $permalink );
    }
    // Make sure that all term links include their parents in the permalinks
    add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 );
    function add_term_parents_to_permalinks( $permalink, $term ) {
    	$term_parents = get_term_parents( $term );
    	foreach ( $term_parents as $term_parent )
    		$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );
    	return $permlink;
    }
    // Helper function to get all parents of a term
    function get_term_parents( $term, &$parents = array() ) {
    	$parent = get_term( $term->parent, $term->taxonomy );
    
    	if ( is_wp_error( $parent ) )
    		return $parents;
    
    	$parents[] = $parent;
    	if ( $parent->parent )
    		get_term_parents( $parent, $parents );
        return $parents;
    }

    Note: after making all changes don't forget to to flush and regenerate the rewrite rules database From WordPress Dashbord -> Settings -> Permalinks and just click Save Changes without any changes.

    Best,
    Ivan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.