Skip Navigation

[Résolu] How to show related custom post types

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: On a single post, I would like to show a list of related posts from the same post type. Cars are organized using three taxonomies - Make, Model, and Series. I would like to show all the cars in the same Make and Model as the current car.

Solution: For the most flexibility, use a 4th taxonomy "Related Vehicles" and create a term for each car, where the term slug matches the car's post slug. Create a View filtered by Related Vehicle, where the term is set using a shortcode attribute. Use the current post's slug in the shortcode attribute to display a list of posts related to the current post.

For more simplicity, you can use the two taxonomies Make and Model to filter a View of Cars, where the terms are set by the current post. This approach will not allow arbitrary relationships.

This support ticket is created Il y a 6 années et 7 mois. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Auteur
Publications
#757605
toolset1.jpg

Hi guys,
I'm currently working on a car website that shows vehicle specifications and other useful/relevant information to motoring enthusiasts.

The overall objective for this website, is to create a complete database of motor vehicles where users can search and perform research against certain makes and models. I figured Toolset would be a perfect solution for this and so far it's been fantastic.

However I'm a bit lost with how best to show 'related vehicles' as a sidebar widget when viewing a vehicle profile page.

So far I have created a custom post type called 'vehicles' and begun putting my vehicle profile pages together. These pages show a brief overview of the vehicle, along with custom fields which display engine, transmission, fuel type, suspension, brakes etc.

I've read numerous instructions on the Toolset website explaining how best to do this without any luck. I did upgraded to the latest beta version (as it supports post relationships) however when trying to set relationships between the same custom post types I'm prompted with a message that reads "Relationships between the same post type is not yet supported" so I'm still confused, and haven't yet been able to figure this out.

To help you understand what I'm trying to do - cars are broken down into the following categories...

Make
Model
Series

An example might be

Make - Ford
Model - XR
Series - Falcon

Typically the Make and Model remain the same, but the series changes.

For eg.

XR Falcon
XR Futura
XR Fairmont
XR GT

So when someone is viewing say, the XR Falcon, the related vehicles that they would see in the sidebar would be XR Futura, XR Fairmont, XR GT

Unfortunately I cant show you a live example of this as I'm currently working on localhost, but I have attached a screen shot below. I am running the latest version 4.9.5 of a default install of WordPress, running the twenty sixteen theme.

No other plugins are active

Looking forward to your reply.

#763412

Hi, the best way to accomplish this is to use a custom taxonomy. I'll call that taxonomy "related-vehicles". Let's assume you want to relate the XR Fairmont and the XR GT Vehicle posts, as an example. Here are the steps you would take:
- Create the XR Fairmont post
- Create the XR GT post
- In the XR Fairmont post, add the related-vehicles taxonomy term that matches the slug of the XR GT vehicle post. For example, if the XR GT post's slug is "xr-gt" then you will add the related-vehicles taxonomy term "xr-gt". Now the XR GT post is related to the XR Fairmont post. Add any other terms for other Vehicles you want to associate with the XR Fairmont.
- In the XR GT post, add the related-vehicles taxonomy term that matches the slug of the XR Fairmont post. Now the XF Fairmont post is related to the XR GT post. Add any other terms for other Vehicles you want to associate with the XF GT post.
- Create a View of Vehicles, filtered by the related-vehicles taxonomy, where the term slug is set by a shortcode attribute "wpvrelatedvehicles". In the Loop Output editor, insert a link to the Vehicle using the wpv-post-link shortcode.
- Insert this View of Vehicles in the Content Template for Vehicles, and pass the current post's slug into the shortcode attribute like this:

[wpv-view name="your-related-vehicle-view-slug" wpvrelatedvehicles="[wpv-post-slug]"]

Now what you have is a View of Vehicles, filtered by the related vehicles taxonomy, where the slug of the taxonomy term matches the slug of the current post. On the XR Fairmont post, you'll see any Vehicles that have the term "xr-fairmont". Add that term to any Vehicles you want to appear on the XR Fairmont post. This approach will continue to work in the new system, and will not be affected by other post relationships.

#764984

Thanks Christian.
I've tried to follow your instructions above as best I could, but I'm still not quite there.

Here's what I have done.

Firstly I created a taxonomy and called it 'related-vehicles'. The slug is related-vehicle. I have set it to a Hierarchical type, and assigned it to post type 'vehicles'

- Create the XR Fairmont post
Done. The URL is lien caché

- Create the XR GT post
Done. The URL is lien caché

- In the XR Fairmont post, add the related-vehicles taxonomy term that matches the slug of the XR GT vehicle post.
Done. I have it set to 1966-xr-ford-falcon-gt

- In the XR GT post, add the related-vehicles taxonomy term that matches the slug of the XR Fairmont post.
Done. I have it set to 1966-xr-ford-falcon-fairmont-sedan

- Create a View of Vehicles, filtered by the related-vehicles taxonomy, where the term slug is set by a shortcode attribute "wpvrelatedvehicles".
Ok. This is where Im confused. What do I select for "This View will display"? Do I set it to the custom post type 'vehicles', or do I set it to the taxonomy 'related-vehicles'?

In the Loop Output editor, insert a link to the Vehicle using the wpv-post-link shortcode.
Err, Ok this is where I get a bit confused as well There's 3? I see loop editor, templates for this view, and output editor. Im going to assume that I put that bit of code into the last one, yes?

- Insert this View of Vehicles in the Content Template for Vehicles, and pass the current post's slug into the shortcode attribute like this:
Hmm, I want to show the related vehicles in the sidebar as a widget.

Thanks for your help dude, sorry Im new at this - but trying!

Also lastly, I have some concerns about scale using this method. I intend on potentially listing tens of thousands of cars over time, and need to ensure that this is going to be a viable method moving forward.

#769277

Ok. This is where Im confused. What do I select for "This View will display"? Do I set it to the custom post type 'vehicles', or do I set it to the taxonomy 'related-vehicles'?
The View will display a list of links to Vehicle posts, so set it to the custom post type Vehicles.

Hmm, I want to show the related vehicles in the sidebar as a widget.
No problem. You can use an HTML widget and insert the View shortcode in the widget instead of inserting it in the Content Template for Vehicles. You'll still use the current page's slug in the shortcode attribute.

I have some concerns about scale using this method. I intend on potentially listing tens of thousands of cars over time, and need to ensure that this is going to be a viable method moving forward.
I can understand that concern. In terms of performance, WordPress taxonomy tables are well optimized and indexed so query speed shouldn't be a problem. This approach is the most flexible I can think of, meaning that related vehicles are arbitrary and not dependent upon the Vehicle's Make or Model or Series. However, it also requires a bit of tedious work to create all the reciprocal links and terms.

If you do not think the flexibility is necessary and you would like a more automated approach, you can use 2 different custom taxonomies - Make and Model. Create terms for each Make and each Model, and assign those terms to each Vehicle. Then your View of Vehicles will be filtered by both the Make taxonomy and the Model taxonomy, where the term is set by the current post. This will show a list of Vehicles in the same Make and same Model as the current post.

#852225

Please dont close this discussion, Im coming back to it!

#857049

Sure, I have marked this ticket as pending an update from you. No need to reply right now. The ticket will remain open for 30 days.

#901416

Ok I had another shot at this and Im still completely lost.

I cant help but think this is incredibly complicated and won't make sense once I have thousands of vehicles listed.

Cant I just show related posts based upon categories???

#901702

Cant I just show related posts based upon categories???
Yes, of course. You'll still have to apply the correct category terms to each post. The approach I was describing only makes it easier to relate posts with arbitrary flexibility. For example, if you want to relate two similar cars that are not in the same make or model - e.g. twinned vehicles Buick Enclave / GMC Acadia. If that flexibility is not required, then yes, it's probably overkill. See my comment from before:

If you do not think the flexibility is necessary and you would like a more automated approach, you can use 2 different custom taxonomies - Make and Model. Create terms for each Make and each Model, and assign those terms to each Vehicle. Then your View of Vehicles will be filtered by both the Make taxonomy and the Model taxonomy, where the term is set by the current post. This will show a list of Vehicles in the same Make and same Model as the current post.