On my client's website that we are building in a sandbox, I have set up a many to many relationship using an intermediary custom post type. We are using Woocommerce and the products are Machines. The three CPTs are Companies, Contacts and Ownership (the intermediary post type).
(fyi but not relevant: we have injected the company and contact post types with the WooCommerce product taxonomy using code in the functions file)
Companies own Machines. Contacts are children of Companies. So the Company CPT is the parent of the Contact CPT and the Ownership CPT.
We've built some searches for the client so he can find the Machines and Companies he needs. You can see the searches on his site here: hidden link
Select the Company Search and you'll see the sample companies we've set up. Click the company name and you'll see the company record with it's custom fields. I want to show the Machines and Contacts associated the Company and I don't know how to tell the Layout to show those associated post types.
Can you guide me please?
After following your detailed instructions I reached "pm/pm-company/abc-test-company/".
So, this is your Company Post, correct?
You want to show the Machines (Product Post Type) and Contacts (Custom Post Type) associated to the Company (Custom Post Type) "abc-test-company".
Since Companies seem to be parent to the Machines and Contacts are children of Companies, you can do this:
Create a view that queries Contacts and Machines and has a Query Filter like "Select posts that are children of the Post where this View is shown." and insert it on that Post (Or in it's assigend Layout)
Let me know if you need more assistance with this.
Hi. I added two additional view cells to the company view: contacts relating to the a company and machines related to a company. I do a company search and click on the company name and get the output with the contact but the view to display the machine does not return any results. When i look at the view cell, there is a message in the Query Filter box:
" This will filter out posts of the following types, because they are not children of any other post type: Products"
I added a couple of Machines (products) to ABC Test Company but, of course, they don't show.
The contact filter works great.
How can I make this work?
Thanks.
Well, you instructed me that Products are set to be Children of Companies?
This should be set in Toolset > Post Types > Companies > Child Post Types.
Is that so?
Then the View will work.
Please can you correct the setup or instruct me with the correct one, so I can come up with a refactored solution?
Hi. I went away for a few days for a family wedding and now I'm back to finish this!
My settings are Toolset > Post Types > Companies > Children Post Types > Contact
AND
Toolset > Post Types > Companies > Children Post Types > Ownership
I set up a staging site. The Company search page is here: hidden link
(I've removed Layouts and am using only Views and Content Templates now).
There is list of Companies (and Contacts on the page).
When I click on the ABC Test Company, I get the Company information (from the Content Template) but none of the Machines (products) associated with the company.
My Template for Companies is:
[wpv-post-title]<br />
[types field='company-street-address'][/types]<br />
[types field='company-address-2'][/types]<br />
[types field='company-city'][/types], [types field='company-state'][/types] [types field='company-zip-code'][/types]<br />
[types field='company-country'][/types]
<div class="row">
<div class="col-sm-4">Phone: [types field='company-phone-number'][/types]</div>
<div class="col-sm-4">Fax: [types field='company-fax-number'][/types]</div>
<div class="col-sm-4">Website: [types field='company-website'][/types]</div>
</div>
[wpv-view name="machines-related-to-a-company" order="asc"]
The View (above) should select posts that are children of the post where the View is shown.
Should I be building this differently?
My settings are Toolset > Post Types > Companies > Children Post Types > Contact
AND
Toolset > Post Types > Companies > Children Post Types > Ownership
This means, OwnerShip is child of Company and Contact Child of Company.
I still miss your post type "Machines (products)".
They do not figure in above schema.
Can you elaborate where in above schema the Post Type "Machines (products)" is associated to?
Thank you
Hi. Ownership is the intermediary Post Type in the many-to-many scenario. Product and Company are both parents of Ownership (set on the Ownership Post Type screen).
On the Company Post Type screen, Children are Contact and Ownership.
On this page, I want to show Companies. When the user clicks a Company, I want to show the associated Contacts and then the Products (machines) :
hidden link
I have a Content Template for the Company; that's what is controlling the Company page (this one, for example:
hidden link
I don't know how to show the associated Contacts and Machines on the page.
Thank you!
Great, so let's sum this up:
Company
-- Contact
-- OwnerShip
Product
-- OwnerShip
I have a Content Template for the Company;
I don't know how to show the associated Contacts and Machines on the page.
You will need to do this:
- For Contacts, simply create a View, that Queries the Contacts, and set a Query Filter to "Select posts that are children of the Post where this View is shown."
Insert this View to the Content Template, and it should Show only Contacts that are Child to the Current Company.
- For Machines (Let's stick to what they are: Products Post Type) you will create a View that Queries OwnerShips.
Set a Query Filter just like previously:
"Select posts that are children of the Post where this View is shown."
Then, in the View Loop, call the PARENT Fields of the Type "Products".
For example, the Post title of the associated Product will be called like this:
[wpv-post-title id="$toolset-parent"]
You can set that with the GUI, and it will, of course, use the Product Post Type Slug, instead of "toolset-parent".
Now insert this View as well to the Content Template of the Company, and you will see a List of Product Data associated to it.
Please do not hesitate to ask more help and specifications in case this was not helpful!
Hi. Thanks so much for working through this with me. The Company CPT Content Template page now shows the associated Contacts (cpt) and Machines (woo Products):
hidden link
I created a custom template page for the CPTs Company and Contact to make them full width and to get rid of the sidebars:
single-pm-company.php and single-pm-contact.php
and put those in the child theme folder.
If you'd like, here's a short video I made for my client to show him how it works.
hidden link
It's a pretty cool solution with a many to many relationship and Contacts injected with the Machine taxonomy. Standard WordPress Users would't support what we needed so this worked out great.
Thank!
Hi. My client is adding Companies and Contacts to the site and it's working fine. The intermediary CPT Ownerships ties the Machines to Companies. When I do a Machine search, the view includes the Company and Ownership type but nothing shows there. I can view a Company and its owned Machines show but the Company doesn't show in the Machine listing.
The site is here:
hidden link
Select the Machine Search button and you can see all the Machines. There are Ownerships between all Machines and their associated Companies but it doesn't show on the Machine Search.
If you do a Company Search and select a Company, its Machines are listed at the bottom of the content template.
Here is the loop from my Machine Search:
<td>[wpv-woo-product-image]</td>
<td>[wpv-post-link]</td>
<td>[wpv-post-field name="_sku"]</td>
<td>[wpv-post-taxonomy type="product_cat" separator=", " format="link" show="name" order="asc"]</td>
<td>[wpv-woo-product-price]</td>
<td>[types field='pm-machine-asking-price'][/types]</td>
<td>[types field='pm-machine-size-a'][/types]</td>
<td>[types field='pm-machine-size-b'][/types]</td>
<td>[types field='pm-machine-size-c'][/types]</td>
<td>[types field='pm-machine-size-d'][/types]</td>
<td>[wpv-post-field name="company_name"]</td>
<td>[types field='pm-machine-ownership-type'][/types]</td>
What am I missing?
Thanks!