Skip Navigation

[Resolved] Get a friendly and dynamic Title and Slug for a Intermediary Post Type

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

Problem:
The customer asked how to set the intermediary post title, based on the titles of the parent and child posts.

Solution:
Guided that to set a custom title and the post slug for the intermediary post, you can use the 'toolset_association_created' hook.
( example code snippet available in the reply: https://toolset.com/forums/topic/get-a-friendly-and-dynamic-title-and-slug-for-a-intermediary-post-type/#post-2292663 )

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

This support ticket is created 2 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by jorgel-4 2 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2292245

Hello,

I'm trying to get a website like this hidden link

I find this way by the moment:

I created two CPT's:
- Servicios (Services) hidden link
- Locaciones (Locations) hidden link

And a many to many relationship: Servicios-Locaciones (Services-Locations) with ¨Intermediary Post Type visible in WordPress admin menu¨ option active. Because this is the most important content I will want to posisionate.

The problem is that the title and the slug for this intermediary post is not friendly. I get a post with the relationship name and the ID's of parents posts:
Title: "Servicios Locaciones: 10 – 11" -
URL: hidden link

But I'm specting something like:
Title: "Albañiles en Barcelona"
URL: hidden link

But created by default and not having to do it manually as the content will be massive.

1) Is it a way of setting this somewhere?

2) Is there another way of getting a URL structure like the example website with Toolset?

Note: The listing items at ¨Servicios¨, that would be the "Services providers", is a content that comes from an external DB. So that's why I didn't not create a CPT for that. (Just to clarify my whole idea.)

#2292663

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To set a custom title and the post slug for the intermediary post, you can use the 'toolset_association_created' hook:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

For example, suppose the relationship slug is 'book-post'. The code to update the post title and slug would for this relationship's intermediary post would look like this:


add_action( 'toolset_association_created', 'custom_intermediary_post_title', 10, 5 );
function custom_intermediary_post_title( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {

	if ($association_slug == 'book-post') {

		$parent_title = get_the_title($parent_id);
		$child_title  = get_the_title($child_id);

		$new_title = $parent_title.' en '.$child_title;

		$updated_data = array(
			'ID' => $intermediary_id,
			'post_title' => $new_title,
			'post_name' => sanitize_title($new_title),
		);
		wp_update_post( $updated_data );

	}
}

You can replace 'book-post', with your target relationship slug and the above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2292751

It worked perfectly.
Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.