Skip Navigation

[Resolved] Using Relationship data in the slug of the Custom Post Type

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the slug of a parent post in the child posts's slug.

Solution: You can use the post relationships API to get information about related posts:

$parent = toolset_get_related_posts(
    12345, // replace this with the child post id
    'relationship-slug', // slug of the post relationship
    'child', // get posts where 12345 is the child post ID
    1000000, 0, // pagination
    array(  ),
    'post_object',
    'parent'
);

Note that the code above can't be used during the save_post hook because the child post is saved before it is associated to a parent. If you want to access parent post information, you can use the toolset_association_created hook. Here's an example updating some information for a child post based on information in its parent post:
https://toolset.com/forums/topic/auto-fill-fields-based-on-related-post/

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by arvidB 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1173516

Tell us what you are trying to do?
> I have a Custom Post Type which has a relationship. Now I want to use the information from the relationship in the slug of the Custom Post. Meaning that for instance the name of the Post is:
Restaurant XYZ

The relationship could be a city name for instance Amsterdam. Now when I save, I would like to use the city name in the slug: amsterdam/restaurant-xyz

At the moment there is a one-to-many relationship from city to restaurant.

Is there any documentation that you are following?
> Older posts I made on the support forum before for similar things using custom fields to set the slug. Here it was mentioned to use ['wpcf']['name of the field'] but I would imagine that WPCF stands for custom field, and as such not sure how to reference the relationship.

Is there a similar example that we can see?
> Not that I'm aware of

What is the link to your site?
> Added this info when I created the support ticket

#1173561

The relationship could be a city name for instance Amsterdam. Now when I save, I would like to use the city name in the slug: amsterdam/restaurant-xyz
Hi, it sounds like you're talking about using the city post slug in the URL directory structure for a child (restaurant) post. Toolset isn't really set up to help you create custom URL directory structures like this. In general, Toolset's custom posts and custom post types fit into the standard WordPress URL directory structure, which looks like this:
https://yoursitename.com/post-type-slug/post-title-slug/
The only way to accomplish what you're asking for in Toolset is to create a custom post type for every city, then add the restaurant post within that city post type. I don't think this is what you're trying to accomplish, exactly. Instead, you may need to look for a third-party custom permalink plugin that helps modify each post's URL somehow. That's not something Toolset is designed to do very well.

Another option is to use a hierarchical post type. For example, in wp-admin WordPress offers the "Page" post type. This is a hierarchical post type that gives you the ability to create directory-based URLs that show the page parent hierarchy. When you create custom post types in Toolset, you have the option of selecting a hierarchical post type. Hierarchical post types are not compatible with Toolset's post relationships system, but come with their own hierarchical system built-in.

Let me know if you have questions about either approach.

#1173564

Thanks Christian for your answer.

I've used the example of restaurant and cities as that was the easiest to explain. But yes, there will always be a relationship between the two, where the child post should have the parent in the slug. I know how to reference custom fields in the slugwhen saving the post, but I can't find documentation on how to include the parent (using the relationship).

#1173617

Again, Toolset can't help you modify URL directory structures like your example. Regardless, you can use the post relationships API to get information about related posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

$parent = toolset_get_related_posts(
    12345, // replace this with the child post id
    'relationship-slug', // slug of the post relationship
    'child', // get posts where 12345 is the child post ID
    1000000, 0, // pagination
    array(  ),
    'post_object',
    'parent'
);

Note that the code above can't be used during the save_post hook because the child post is saved before it is associated to a parent. If you want to access parent post information, you can use the toolset_association_created hook:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

Here's an example updating some information for a child post based on information in its parent post:
https://toolset.com/forums/topic/auto-fill-fields-based-on-related-post/

#1173639

My issue is resolved now. Thank you!