Skip Navigation

[Resolved] Relationship between Taxonomies

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

Problem:
The customer asked what is the best way to make the relationship between two custom taxonomies.

Solution:
Guided that in Toolset, relationships can only be created between the post types.

Shared some workarounds along with some custom code.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 2 years, 9 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)

Author
Posts
#2290399

Tell us what you are trying to do? If a user selects a "Town" Taxonomie for his post type (accommodation or Eating place etc...., depending on which town he chooses it should automatically be added to specific "Areas" taxonomy
Does the relationship function seem to only work for pages? Is there an away I can make it use taxonomies

Is there any documentation that you are following? can not find it

Is there a similar example that we can see? do not see it

What is the link to your site: hidden link

#2291499

Hi,

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

Your observation is correct and the relationships can only be used to connect post types. They can't be used to connect taxonomies.

Is there any specific reason for using two separate taxonomies for the towns and areas? One workaround can be to use a single hierarchal taxonomy and add the towns and area terms in the parent-child structure. For example:

- Area 1
-- Town A
-- Town B
-- Town C
- Area 2
-- Town D
-- Town E
-- Town F
- Area 3
-- Town G
-- Town H
-- Town I
......

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2291523

Thank you Waqar

No that would not work. Because 1 town should be allocated to different areas. It is a bit difficult.

This is a tourism site, and the map is divided into area names to make searches per area. The problem is that the local people/ service providers differ about what areas they are in. And I need to think of how the visitors to the site would think, not how the services providers think.

Tourism members will submit their product/service and select the town they are in. So I need a way to automatically group those services pages in different area. If I allow the Services providers will choose the areas them selfs, there is no consistency.

Each town will display in more than one area, so that I provide a better experience for the visitor.

#2292497

Thank you for sharing these details.

In that case, another approach can be to use separate custom post types for the 'Areas' and 'Towns' and connect them all using the post-relationships.

Post Types:
- Areas
- Towns
- Listings

Post relationships:
- Areas <-> Towns
A many-to-many relationship where multiple Areas can be connected to multiple Towns.

- Towns -> Listings
A one-to-many relationship, so that each Listing can be attached to only one Town, but, one Town can be linked to multiple Listings.

When submitting their product/service, members will just select the related Town and it will automatically and indirectly link to the related Areas too.

#2293323

Thank you for the suggestion, but my different post types, is Accommodation, Things to do and places to eat. Can not be towns.

At this stage, I'm exploring creating an IF statement. But not sure how to write it. There are 100 towns, so I know I would have to write 100 IF statements.

Something like :

If category: Towns = cape_town then
category:areas
western_cape => true;
tankwa => true;

#2294485

Managing 'if' and 'else' statements for these many terms, can be inefficient and difficult to maintain in a long run.

I was able to connect 'Towns' and 'Areas' taxonomy terms, using a term field.

1. I added a single line type term field 'town-areas' with the taxonomy for 'Towns' and made the field have multiple instances.

This field will allow you to save the names of all the "Area" terms with each "Town" term so that whenever a particular town is attached to a post, its connected areas terms are also attached to that post.

2. Next, I added a post form to add a new listing post, and in the form, included the "Town" taxonomy field, but no "Area" taxonomy field.

3. In the last step, I added a custom function attached to the 'cred_save_data' hook.
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )

When the form is submitted, this function will get all the attached "Town" terms from the post, then get all the names of the Areas saved in the term field of those terms, and then attach those Area terms, with the created post.

The code snippet below 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:


add_action('cred_save_data', 'custom_town_area_connect_funct',10,2);
function custom_town_area_connect_funct($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==1234)
	{
		$town_tax_slug = 'town-taxonomy-slug';
		$area_tax_slug = 'area-taxonomy-slug';
		$town_area_field_slug = 'town-areas';

		$town_terms = get_the_terms( $post_id, $town_tax_slug );
		if ( $town_terms && ! is_wp_error( $town_terms ) ) {
			$town_term_all_areas = array();
			foreach ( $town_terms as $town_term ) {
				$town_term_ID = $town_term->term_id;
				$town_term_areas = get_term_meta( $town_term_ID, 'wpcf-'.$town_area_field_slug, false);
				if(!empty($town_term_areas)) {
					$town_term_all_areas = array_merge($town_term_all_areas, $town_term_areas);
				}	
			}
			if(!empty($town_term_all_areas)) {
				wp_set_post_terms( $post_id, array_unique($town_term_all_areas), $area_tax_slug );
			}
		}
	}
}

Note: In this custom function, you'll replace:

1234: with the ID of your post form
town-taxonomy-slug: with the taxonomy slug for the towns
area-taxonomy-slug: with the taxonomy slug for the areas
town-areas: with the term field slug, used to store the connected Area term names with the Town terms.

I hope this helps and please let me know if any step is not clear.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2295091

My issue is resolved now. Thank you!