Skip Navigation

[Resuelto] Automatically connecting post types in many-many relationship

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I have two post types: Studios and Artists. A Studio can have many Artists, and an Artist can belong to many Studios (set up as a many-many relationship). I have Toolset forms to create each post type.

However, I cannot work out how to add a field to the Artist CRED form that automatically creates a link to the current Studio - I want to use the Artist Toolset form in the content template for a Studio.

Solution:

It is possible with some custom codes, for example:

https://toolset.com/forums/topic/automatically-connecting-post-types-in-many-many-relationship/#post-1074784

Relevant Documentation:

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

This support ticket is created hace 6 años, 3 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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/Hong_Kong (GMT+08:00)

Etiquetado: 

Este tema contiene 5 respuestas, tiene 2 mensajes.

Última actualización por Luo Yang hace 6 años, 3 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1071702

I have two post types: Studios and Artists. A Studio can have many Artists, and an Artist can belong to many Studios (set up as a many-many relationship). I have CRED forms to create each post type.

However, I cannot work out how to add a field to the Artist CRED form that automatically creates a link to the current Studio - I want to use the Artist CRED form in the content template for a Studio. How would I achieve that? Is it possible, or have I overlooked some piece of documentation?

#1072471

Hello,

For the many-to-many relationship, we have provided the relationship form to connect existed posts, see our document:
https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

#1074726

Hi Luo,

Thanks; I'm aware of the creating of relationship forms.

What I'm asking is how to add a field to the Artist CRED form that automatically creates a link to the current Studio being viewed. It's an unneccesary extra step to make people fill out an extra form to link the two posts- especially since I have done this before for post types with a one-many relationship, just not many-many.

Cheers.

#1074784

Hello,

As you can see there isn't such a built-in feature within Toolset form in many-to-many relationships.

But it is possible with some custom codes, for example:
1) You are using below many-many relationship, the relationship slug is "studio-artist", you can get the relationship slug by editing the many-many relationship, make sure "studio" post type is in the left place of the relationship, and "artist" post type is in the right place.

2) In the single "studio" post, you can display a Toolset post form for creating "artist" post

3) In the Toolset post form, you can get the current "studio" post ID with Views shortcode
[wpv-post-id], use it in a hidden field "studio_id", and pass parameter to Toolset form, for example:

[cred_generic_field field='studio_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-id]"
}
[/cred_generic_field]

https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

4) Use action hook cred_save_data to trigger a custom PHP function, in this function connect the "studio" post with the new "artist" post, like this:

add_action('cred_save_data', 'connect_studio_artist_func',10,2);
function connect_studio_artist_func($artist_id, $form_data)
{
    // if a specific form
    if ($form_data['id']== 35 && isset($_POST['studio_id']))
    {
		$studio_id = $_POST['studio_id'];
		if($studio_id){
			toolset_connect_posts(
				$relationship = 'studio-artist',
				$studio_id,
				$artist_id
			);
		}
    }
}

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#1075176

Perfect Luo, that's exactly what I was after. Cheers!

#1075196

You are welcome