Skip Navigation

[Resolved] Show matching CPT on single CPT view

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 10 replies, has 2 voices.

Last updated by Luo Yang 1 year, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#2607263

Hi, thank you for reading.

I have a website with 2 custom post types: "keebs" and "keycaps"

keebs CPT has a custom field of "keeb-keycap-brand"
keycaps CPT has a custom field of "manufacturers"

Both custom fields are checkboxes that have the same options.

I would like it so when a user is on a keebs single page that there is a view on the bottom of the page that shows all keycaps CPT that match.

i.e. keebs CPT has "keeb-keycap-brand" of "Company1" I would like a view on the single view of this that shows all the keycaps CPT that have a matching "manufacturers" of "Company1"

Can I achieve this with toolset?

#2607339

Hello,

In your case, it is a many-to-many relationship between post types "keebs" and "keycaps"
It is not recommended to a custom checkboxes field to setup the relationship.

In your case, you can try with a custom taxonomy, for example:
1) Create a taxonomy "keebs-keycaps", register it to both post types "keebs" and "keycaps"
2) In single "keebs" post, display nested view:
a1) Parent taxonomy view:
- query terms of taxonomy "keebs-keycaps"
- Filer by:
Taxonomy is set by the page where this View is inserted
- In views loop, display below child post view

a2) Child post view:
- query "keycaps" posts
- Filer by:
Select posts with taxonomy:
Categories set by the parent Taxonomy View
- In views loop, display "keycaps" post information

#2607569

Thank You for your response. I fail to see how this does what I'm looking to do though. If I'm assigning all keycaps and keebs to the same category of keebs-keycaps how does it match up the manufactures and keeb-keycap-brand custom fields to query?

#2607619

I think Shane helped another user out with a similar issue: https://toolset.com/forums/topic/filter-view-by-shortcode-based-on-custom-field-values/

Maybe we could loop them in?

#2607675

I got it to work through your suggestion. I was confused with the taxonomy at first, but I got it to work! Thank You. I'll reach back out if I have any other issues.

#2607707

Ran into another issue. When I have a CPT that has multiple categories in one taxonomy. Let's say the taxonomy is "keebs-keycaps-colors"

When I have a keeb that has multiple colors like blue and gray. The parent/child view shows 2 views. 1 for blue and one for gray. I would like them combined into 1.

#2607747

Yes, it is possible with only one post view, see below sandbox website.
Login URL: hidden link

1) Post view:
- Query keycaps posts
- Filter by:
Select posts with taxonomy:
keebs-keycaps-colors the same as the page where this View is shown
- In view's loop, display keycaps post in formation

2) Test it in single keebs post:
hidden link
It works fine

For your reference.

#2607963

Thank You. That worked great!

Is there a disadvantage/advantage to using either method?

Also can I control if it's an AND or OR relationship for the view?

#2607985

The first method is using nested views, the second one is just one post view, you can choose one of them depends on your need.

You can customize the AND or OR relationship by following our document:
https://toolset.com/de/course-lesson/filtering-custom-lists-of-posts/#filtering-by-taxonomy

And you can use wpv_filter_query filter to customize the taxonomy filters:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

#2608009

Thank You for those. In the first link it only shows how to do it when you are setting the categories directly, not if they are set by the page the view is being used on.

#2608035
and-or.jpg

The "And or Or" option only available when you add multiple taxonomy filters, see my screenshot