[Resolved] Intermediary CPT slug

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.

This topic contains 4 replies, has 2 voices.

Last updated by umbertoZ 6 months ago.

Assigned support staff: Waqar.

Author
Posts
#1351631

Hi, I've created a ManyToMany Relationship and I set intermediary CPT as visible on my WP-admin menu.

I want to display on the frontend the single page of the intermediary CPT. I see that the default slug of that page is /relationshipSlug-postID2-postID2/.

Is it possible to edit that slug to get /postSlug1-postSlug2/?

Thanks

#1352327

Waqar
Supporter

Languages: English (English )

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

Hi there,

Thank you for waiting, while I performed some tests on my website.

To update the title and the slug of the created "intermediary" post, you can hook a custom function to "wp_insert_post":
https://developer.wordpress.org/reference/hooks/wp_insert_post/

Example:


function hook_for_intermediary_post( $post_id, $post, $update ) {

	// execute only if a new post is being inserted
	if ($update == false) {

		// post-relationship slug
		$relationship_slug = "club-member";

		// If this is a revision or not the post that we need to target, return.
		if ( (wp_is_post_revision( $post_id )) || ($post->post_type != $relationship_slug) )
			return;

		// default slug of the created post
		$post_existing_slug = $post->post_name;

		// extracting parent and child post IDs from the slug
		$post_existing_slug_arr = explode('-', $post_existing_slug);

		$parent_id = $post_existing_slug_arr[2];
		$child_id = $post_existing_slug_arr[3];

		// getting title and slug of the parent post
		$parent_title = get_the_title($parent_id);
		$parent_slug = get_post_field( 'post_name', $parent_id );

		// getting title and slug of the child post
		$child_title = get_the_title($child_id);
		$child_slug = get_post_field( 'post_name', $child_id );

		$update_post = array(
			'ID'			=> $post_id,
			'post_title'	=> $parent_title." ".$child_title,
			'post_name'		=> $parent_slug."-".$child_slug,
		);

		// Update the post into the database
		wp_update_post( $update_post );

	}
}
add_action( 'wp_insert_post', 'hook_for_intermediary_post', 10, 3 );

The above code snippet can be inserted in the active theme's "functions.php" file and please replace "club-member" with the actual relationship slug, used on your website.

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

#1353775

Hey, thank you very much for the code. It works great. Just an issue. It wasn't working, so I had to comment these lines to make it work:

// If this is a revision or not the post that we need to target, return.
//      if ( (wp_is_post_revision( $post_id )) || ($post->post_type != $relationship_slug) )
//          return;
 

What can happen if this is commented?

After creating the connection (and the creation of the intermediary post) I will update the value of a custom field associated to the intermediary CPT using some PHP triggered by a cred form. It will be a counter where I add +1 everytime a user will Submit the form. Do you see any problem about this?

thanks

#1353893

Waqar
Supporter

Languages: English (English )

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

Hi,

Thanks for the update and glad that it works.

The commented out lines are important since they make sure that the function executes only when a specific post type ( intermediary post ) is inserted. Removing that part can make the function execute for all post types.

Have you tested this with the correct intermediary post type slug, in place of "club-member"? Also can you please make sure that you're testing the function by creating a relationship from the parent or child post's edit screen?

As for updating the custom field value programmatically, it should be fine, since you'll be using the post ID for that and this function only changes the title and the slug.

regards,
Waqar

#1353895

Hey!! It was my fault!!

The intermediary post type slug was incorrect.

You were right.

Great job, Thanks a lot