Home Forums General Issues Relationship slugs witha custom post type Reply To: Relationship slugs witha custom post type

  • Did you ever figure this out? I want to achieve exactly the same!

    Right now, I have the following setup (I used item & collection to match your example);
    In my post type I have the following rewrite set:

    'rewrite' => array('slug' => 'item/%collection%'),

    And a function that replaces %collection% for the acf value:

    function setupItemPermalink($post_link, $id = 0) {
    	$post = get_post($id);
    	if (is_object($post) && get_post_type($post) == 'item') {
    		$collection = get_field('item_collection', $post->ID);
    		if ($collection) {
    			$collection = get_post($collection[0]); // get first in array
    			return str_replace('%collection%' , $collection->post_name, $post_link);
    		} else {
    			$other = sanitize_title(__('not-in-collection', 'mydomain'));
    			return str_replace('%collection%' , $other, $post_link);
    	return $post_link;
    add_filter('post_type_link', 'setupItemPermalink', 1, 3);

    This part works! When I use the_permalink for an item, it outputs:

    But I’m not sure how I should setup my rewrites?
    This is not working:

    global $wp_rewrite;
    $item_structure = '/item/%collection%/%item%/';
    $wp_rewrite->add_rewrite_tag('%item%', '([^/]+)', 'item=');
    $wp_rewrite->add_permastruct('item', $item_structure, false);

    Any ideas?