Skip Navigation

[Resolved] Connecting CPT using Relationship or Custom Field

This support ticket is created 2 years, 8 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
#2146859

Tell us what you are trying to do?

Firstly, greatly appreciate your support so far with using toolset. I'm new to WordPress and still learn how to use this plugin.

I have a "Project" CPT listing real estate projects and data, including companies involved (developer, architect, contractor, etc.). I also created another CPT "Companies" with company profiles and contact info.

Now, I'm trying to create a dynamic list called "Companies Involved" listing the role (developer, architect, etc.) and the Company (linked to the company profile) in the "Projects" Custom Template

Please kindly advise :

a) Should I create a CPT Relationship linking Projects CPT and Companies CPT? If yes, how can I assign the Companies to each role (developer, architect, etc)? Do I need an intermediary CPT "Companies Involved/Roles"? How to list if a company could have multiple roles (architect & engineer)? To goal is also to have an archive page for Companies listing their involved projects.

b) or should I create a Custom Field and use php to create a drop-down list of Company CPT for each role? If yes how to do this (I'm not yet familiar with the process of adding custom code). I assume with this method "Companies" cannot have an archive page?

Is there any documentation that you are following?

I've been watching the CPT Relationship videos & documentation & scanning other support discussions. Some mentioned using adding code to function.php but I'm not familiar with php.

Is there a similar example that we can see?

An example can be found here (scroll to table "CTBUH MEMBER COMPANIES INVOLVED")
hidden link

What is the link to your site?

hidden link

Thank you.

#2147311

Waqar
Supporter

Languages: English (English )

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

Hi,

Thank you for contacting us and glad that our support is proving useful.

Both the approaches that you've shared can work, however, there are some limitations associated with them:

1. You can create a many-to-many relationship between the "Projects" CPT and the "Companies" CPT and for roles (developer, architect, contractor, etc.), you can add a select type custom field to this relationship's intermediary post.

However, in this approach, you won't be able to create more than one relationship between the same "Project" and "Company" post. This means that if you've created 1 relationship between a "Project ABC" and "Company XYZ" with a "developer" role, you won't be able to create another relationship between these two posts, with another role, like "architect" or "contractor".

2. Alternatively, you can add a select type custom field for each role (developer, architect, contractor, etc.) with the "Projects" CPT and dynamically generate their options from the available posts in the "Company" CPT using the "wpt_field_options" filter:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

You'll find more details on this topic with a code example, in this reply:
https://toolset.com/forums/topic/create-a-family-relationship-between-contacts/#post-1515825

The downside of this approach is that you won't be able to perform a front-end search in your views for the projects to filter the results based on these role fields.

3. Based on the limitations shared above, my recommendation would be to add custom taxonomies with the "Projects" CPT, for each of these roles:

1. Project Developers
2. Project Architects
3. Project Contractors

This will allow you to attach the same company with a project in more than one role, use the project company attachment in the front-end search, and it will also support custom archive pages for each company in each role.

The challenge in this approach would be that whenever you'll add a new company to the "Companies" CPT, you'll also have to add its term in the related custom taxonomies for each role. As long as you're adding the terms with the same title and slugs in the custom taxonomies and the "Companies" CPT, this should work.

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

regards,
Waqar

#2147527

Thanks Waqar for the swift response. I'm still a bit confused, please help to clarify in case I go with options 3. custom taxonomies for each role:

1. I'll have dozens of roles for Developer, Architect, Structure Engineer, MEP Engineer, Landscape, etc . E.g.:

Project: Landmark 81
- Architect: Atkins
- MEP Engineer: Aurecon
- Structure Engineer: Aurecon
- Fire Engineer: Aurecon

How can I add "Atkins" to the Project Architect taxonomy? Can I select these companies from a drop-down within the taxonomy list (is it a term field?)
How can I add "Aurecon" to multiple roles? Do have to select Aurecon multiple times from a drop-down or checkbox?

2. "The challenge in this approach would be that whenever you'll add a new company to the "Companies" CPT, you'll also have to add its term in the related custom taxonomies for each role." Do I have to manually update? I expect to have a list of 200 projects and 100 companies.
Let's say I add a new company "Arup", I suppose creating a term field is sufficient and then I can select "Arup" to update current projects?

Thank you.

#2147673

Waqar
Supporter

Languages: English (English )

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

Thanks for writing back.

To make this third approach clearer and answer your recent questions, I've created a temporary staging website at:
hidden link

a). I added two custom post types:
- Projects:
hidden link
- Companies
hidden link

b). Next, I added 4 custom taxonomies and attached them to the "Projects" post type:
- Project Architects
hidden link

- Project MEP Engineers
hidden link

- Project Structure Engineers
hidden link

- Project Fire Engineers
hidden link

c). Next, I added 5 example companies in the "company" post type:
- Atkins
- Aurecon
- Company A
- Company B
- Company C

d). I also added these 5 company names as terms in each of those 4 custom taxonomies.

e). Now, when you'll go to the edit screen to add a new "Project" post or edit the existing one ( Landmark 81 - hidden link ), you'll see that the fields to select each custom taxonomy term is available in the sidebar at the right side.
( screenshot: hidden link )

As you'll start typing, the company names which have been added already as terms will become available for selection.

f). Suppose, you have to add a new 6th company "Company D" to the website. You'll go to the "Companies" post type and add it as a new post. After that, you'll go to the custom taxonomy management pages for all those 4 taxonomies and also add "Company D" as a term in each of them manually.

I hope this demo website will make it more clear and answers your questions.

#2148745
Picture1212.png
Picture411.png
Picture91.png
Picture11.png

Hi, thanks for the demo site. It is very helpful.

I've implemented your advice on my site and still have a few queries (please see attachments):

1. To display Companies in the Project CPT, I've used Single Field block -> taxonomy -> developer or architect term, with links to the archive. But he archive of the company only shows the Company in it's specific role (e.g. developer/architect) but not all roles. Can I create link directly the Company CPT template to display the company info, list of projects and roles involved in each project?

2. How do I connect the Companies CPT to my database listed in Project CPT? E.g. searching how many projects company A has been involved with and as which role? Do I create page + view or Companies custom archive?

I've also attached a sample webpage to illustrate what I'm trying to achieve.

If you time please kindly look at my site. I've created an account for toolset support.

Thank you.

#2149349

Waqar
Supporter

Languages: English (English )

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

Thanks for the update and glad that we're making progress.

Can you please share temporary admin login details (username and password) for the Toolset admin user, in reply to this message?

I'll be in a better position to guide you with the next steps accordingly.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#2149791

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing the admin access.

1. If you'd like to link the term names for the companies in different roles, to their respective single company post page, you can add some custom redirect code.

For example:


add_action( 'template_redirect', function() {
	// slug of the target company post type
	$company_post_type = 'company';
	// check if the tax archive page is being viewed for the selected taxonomies
	if ( is_tax( array('developer', 'architect') ) ) {
		// get the current taxonomy term's name
		$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
		$term_name = $term->name;
		// get the link to the company post with the same title
		$redirectLink = get_permalink( get_page_by_title( $term_name, OBJECT, $company_post_type ) );
		// redirect to the company post
		wp_redirect( $redirectLink, 301 );
		exit;
	}
});

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.

As a result, whenever visitors will try to visit any company term's archive page (in any role), it will redirect automatically to the relevant single company post page.

2. To show the list of all projects where the current company has any role, on the single company post page, you'll need to create a post view, using the classic/legacy views editor.

Currently, there is no content template assigned to the single company posts, so the first step would be creating a content template for this post type.

Next, you can go to WP Admin -> Toolset -> Settings -> General and change the editing experience option to show both the legacy and the blocks interface. After that, you'll be able to create the legacy views from WP Admin -> Toolset -> Views. You'll create a view that shows the "Project" posts, filtered by the taxonomy term title from the current company post.

You'll find the guides on working with the legacy views at:
https://toolset.com/documentation/legacy-features/views-plugin/

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

#2150819

Dear Waqar,

Thank you, the custom code works great. I was able to set it up smoothly. I'll try to populate more data on the site and create the legacy views as recommended. I have a few final queries:

1. On your demo site you've set Project Architect (and other roles) taxonomy type as Flat. Can I set to to Hierarchical to to select from a check box? I'd like to create a Hierarchical taxonomy called Roles used with Projects; then under Roles I create Architect, Developer, etc. (similar to Categories) to keep the admin clean since I expect to have 20 roles;. Would that work with your recommended set up?

2. If I want to list of projects by city and function (residential, office, retail), should I add another custom taxonomy (city, function) or should I create custom fields instead and let users search front-end?

3. To manage the hundreds of company that I'll add, I'd also like to categorize and tag them for search. I suppose the custom code above would still work.

Thank you for your great support.

#2151125

Waqar
Supporter

Languages: English (English )

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

Thanks for the update.

1. Having a unified Roles taxonomy won't work for these reasons:

a). You'll be able to search only by a single role at a time and not by each role independently.
b). You'll not be able to filter the projects by each company's role, because roles information will be stored within the same taxonomy.
c). The company/role term management in this structure would become even more trickier, to manage.

In my proposed set up of individual taxonomies for each role, the structure would be:

Architect Taxonomy
- Company A
- Company B
- Company C
- Company D

Developer Taxonomy
- Company A
- Company B
- Company C
- Company D

Designer Taxonomy
- Company A
- Company B
- Company C
- Company D
.......

If you'll go with a unified single hierarchal custom taxonomy approach it would look like this:

Roles and Companies Taxonomy
- Architect
-- Company A
-- Company B
-- Company C
-- Company D
- Developer
-- Company A
-- Company B
-- Company C
-- Company D
- Designer
-- Company A
-- Company B
-- Company C
-- Company D
.......

Note how you'd still have to repeat each company under each individual role.

2. You can use custom fields or custom taxonomies for storing information like cities and functions and both will allow you to add font-end search filters.

However, it is recommended to choose custom taxonomies over custom fields whenever possible, because taxonomy-based filtering/search has performance benefits and is faster.

3. Yes, the setup that I've suggested would allow you to add front-end search fields for each role taxonomy with the company selection field, so that you can search for projects with respect to companies, in the individual role.

The custom code only redirects taxonomy archive pages to their respective company's single CPT page, which will have all the information about that company, anyway. This will benefit you because you won't have to manage the same company information across multiple taxonomy archive pages.

#2154215

My issue is resolved now. Thank you!

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