Home › Forums › General Issues › Relationship slugs witha custom post type
I created a custom post type ‘collection’ and a custom post type ‘item’.
I used the ACF relationship so when I add an item, I can select under which collection it should be.
add_action( 'init', 'kc_create_post_type_collection' );
function kc_create_post_type_collection() {
register_post_type( 'kc_collection',
array(
'labels' => array(
'name' => __( 'Collection' ),
'singular_name' => __( 'Collection' )
),
'capability_type' => 'post',
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => '/collection'),
'supports' => array( 'title'),
'menu_position' => 21,
'menu_icon' => 'dashicons-tag'
)
);
}
add_action( 'init', 'kc_create_post_type_items' );
function kc_create_post_type_items() {
register_post_type( 'kc_item',
array(
'labels' => array(
'name' => __( 'Items' ),
'singular_name' => __( 'Item' )
),
'capability_type' => 'post',
'public' => true,
'has_archive' => true,
//'rewrite' => array('slug' => '/collection'), ??????
'supports' => array( 'title'),
'menu_position' => 21,
'menu_icon' => 'dashicons-tag'
)
);
}
The slug of collection is like http://www.site.com/collection/collection-name
The slug of an item should be something like
http://www.site.com/collection/collection-name/item-name
But how can I create such a slug? Keep in mind that an item can be under more then one collection.
Any ideas? Thanks!
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:
http://www.mydomain.com/item/collection-name/item-slug/
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?
The topic ‘Relationship slugs witha custom post type’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.