Skip Navigation

[Resolved] Relationships > Many-to-Many : more joins

This support ticket is created 3 years, 5 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/Hong_Kong (GMT+08:00)

This topic contains 12 replies, has 2 voices.

Last updated by Luo Yang 3 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#2129069

Hi,

with 2 "Custom Post Types" with a "Many-to-Many" relationship, is it possibile to "join" more times the same records?

I try to give you an example.
We have 2 "Custom Post Types":
- Colors
- Product
and in the "Many-to-Many" relationship I've an Intermediary Custom field "Size"

Then I can have:
- Product: Shoes > Color : Red | Size: Big
- Product: Shoes > Color : Red | Size: Medium
- Product: Shoes > Color : Red | Size: Small
- Product: Shirt > Color : Red | Size: Large
- Product: Shirt > Color : Red | Size: Small
- Product: Shoes > Color : Blue | Size: Small
- Product: Pants > Color : Blue | Size: X-Large

Then if i edit "Shoes" in the "relationship view" I can see:
- Red | big
- Red | medium
- Red | small
- Blue | small

and editing "Red" in the "relationship view" I can see:
- Shoes | big
- Shoes | medium
- Shoes | small
- Shirt | Large
- Shirt | Small

And so on....

Because I tried to link two times a record with another record with which it has already liked but I couldn't do it.

Regards
Alex

#2129521

Hello,

With only one relationship, you can connect same posts only once.

In your case, you can consider below workaround:
1) Setup three post types:
- Colors
- Product
- Colors-Product

2) Setup two one-to-many relationships:
- one-to-many relationship between "Colors" and "Colors-Product"
- one-to-many relationship between "Product" and "Colors-Product"

So you can connect the same same "Colors" and "Product" posts multiple times with the "Colors-Product" post type

#2132833

Ok but where I've to add my Intermediary Custom field named "Size"?
a) one-to-many relationship between "Colors" and "Colors-Product"
b) one-to-many relationship between "Product" and "Colors-Product"
c) in post type "Colors-Product"
?

Regards Alex

#2133347

One more question...
how can I automatic save the relationships:
- "Colors" -> "Colors-Product"
- "Product" -> "Colors-Product"
when I save (or update) a "Colors-Product"?

#2133381

Q1) where I've to add my Intermediary Custom field named "Size"?
You can add the Custom field "Size" into post type "Colors-Product"
https://toolset.com/course-lesson/creating-custom-fields/

Q2) how can I automatic save the relationships:
You need to setup the relationships manually, when you edit a "Colors-Product", you can choose the related "Colors" post and "Product" post manually, after save the "Colors-Product" post, those relationships will be setup automatically.

#2133445

Q2) save automatic relationships:

In "Color-Products" I've set this custom fields:
- Color (Field type: Post Reference > Colors)
- Product (Field type: Post Reference > Products)
- Size (Field type: Text)

When I save (or update) the "Color-Products" I need to save in:
- in "Color" (post type) the association between "Color" and "Color-Products"
- in "Products" (post type) the association between "Product" and "Color-Products"
Because when I open the "Color" or the "Product" item I need to see its all connections.

Example:
In "Color- Product" post type I've this list:

Product	Color		Size
-----------------------------------------
Shoes		Red 		Big
Shoes 		Red 		Medium
Shoes 		Red 		Small
Shirt 		Red 		Large
Shirt 		Red 		Small
Shoes 		Blue		Small
Pants 		Blue		X-Large

"Shoes" in the "relationship view" I need see:

Color		Size
-------------------------
Red 		Big
Red 		Medium
Red 		Small
Red 		Large
Red 		Small
#2133539

To avoid more misunderstandings, please elaborate the questions with more details:

When I save (or update) the "Color-Products" I need to save in:
- in "Color" (post type) the association between "Color" and "Color-Products"
- in "Products" (post type) the association between "Product" and "Color-Products"

When you edit/create a "Color-Products" post, can you associate it with other "Color" post and "Product" post, or not?

#2133613
relationships-product_color.jpg
product-relationships-product_color.jpg
edit-product_color.jpg
custom-fields-product_color.jpg
color-relationships-product_color.jpg

When you edit/create a "Color-Products" post, can you associate it with other "Color" post and "Product" post, or not?

Yes, it is possible.

As you suggested the structure is:
Post types:
- Colors
- Product
- Colors-Product

one-to-many relationships:
- one-to-many relationship between "Colors" and "Colors-Product"
- one-to-many relationship between "Product" and "Colors-Product"

Colors-Product fields:
- Color (Field type: Post Reference > Colors)
- Product (Field type: Post Reference > Products)
- Size (Field type: Text)

Product relationship view:
- Colors-Product (post_title)
- Colors-Product.Product
- Colors-Product.Color
- Colors-Product.Size

Color relationship view:
- Colors-Product (post_title)
- Colors-Product.Product
- Colors-Product.Color
- Colors-Product.Size

I hope the atteched images helps.

Regards

#2134295

Why do you need those two post reference fields in post type "Colors-Product ":
- Color (Field type: Post Reference > Colors)
- Product (Field type: Post Reference > Products)

You have already setup two one-to-many relationships:
- one-to-many relationship between "Colors" and "Colors-Product"
- one-to-many relationship between "Product" and "Colors-Product"

So when you edit edit/create a "Color-Products" post, you should be able to setup the post relationships without post reference fields.

Please provide a test site with the same problem, also point out the problem post URLs

#2135349

I think there are some misunderstandings.

The Toolset post reference field is also based on one-to-many relationship, the relationship slug is same as the post reference field slug, so you have setup too much relationships between those three post types, if you prefer to post reference field, you can remove two one-to-many relationships:
- one-to-many relationship between "Colors" and "Colors-Product"
- one-to-many relationship between "Product" and "Colors-Product"

And in the single "Colors" post, you can setup a view:
- Query "Colors-Product" posts
- Filter by relationship between "Colors" and "Colors-Product"
- In view's loop, display "Colors-Product" post information + related "Product" post information
More help:
https://toolset.com/course-lesson/displaying-related-posts/

Please let me know if you need assistance for it, I can setup a demo in your website.

#2135431

Hi,

"And in the single "Colors" post, you can setup a view:
- Query "Colors-Product" posts
- Filter by relationship between "Colors" and "Colors-Product"
- In view's loop, display "Colors-Product" post information + related "Product" post information
More help:
https://toolset.com/course-lesson/displaying-related-posts/"

It looks a good solution but I don't need it in front-end, I need it in admin in editing the Custom Post Type ("Colors" or "Product") interface .
Is it possible?

I was thinking to add a "custom function" in functions.php on saving/updating a "Colors-Product".
To get the value of "Color" and "Product" in "Colors-Product" and add it in the relative one-to-many relationship.
Example:
- I create a new "Colors-Product" with title "Blue-Shoes"
- "Blue-Shoes" has Color: Blue and Product: Shoes and Size: M
- saving/updating "Blue-Shoes" the function get the "Color" and the "Product" value
- then function adds:
----- in the relationship one-to-many "Color > Colors-Products": a new relationship between "Blue" and "Blue-Shoes"
----- in the relationship one-to-many "Product > Colors-Products": a new relationship between "Shoe" and "Blue-Shoes"

I know how to get the value of "Color" and "Product" but I don't know how to add (by code) the new relationship between "Color" (or "Product") and the "Colors-Products".

Here the code that I used to create the automatic post_title:

add_action( 'save_post', 'update_title_collegamento_pro_pra', 99 );
function update_title_collegamento_pro_pra( $post_id )
{
    if( get_post_type( $post_id ) == 'collega-prof-prat' ) {

         // get Post Reference ids
        $join_pro_pra_professionista 	= toolset_get_related_post($post_id ,'join-pro-pra-professionista');
        $join_pro_pra_praticante		= toolset_get_related_post($post_id ,'join-pro-pra-praticante');
		 
        // do the actual processing
        if (empty (get_the_title( $post_id )) ){
			
			// create a new post_title
			$join_pro_pra_title = get_the_title( $join_pro_pra_professionista ) . " - " . get_the_title( $join_pro_pra_praticante );
			
			// set new post_title
			$my_args = array(
				'ID' => $post_id,
				'post_title' => $join_pro_pra_title,
				'post_name' => sanitize_title($join_pro_pra_title)
			);

			// update the post, which calls save_post again
			$res = wp_update_post( $my_args, true );
		};
    }
}
#2135577
#2137181

Yes, you can use Types API function toolset_connect_posts() to connect posts with custom PHP codes, it depends on yourself.

But as I mentioned above:

Toolset post reference field is also based on one-to-many relationship, the relationship slug is same as the post reference field slug, so you have setup too much relationships between those three post types,