Skip Navigation

[Resolved] Dynamic link button to relationship between post type

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

Problem: In the results of an archive of a parent post type, I would like to display buttons to link to each related child post.

Solution: Use a nested View of the child post type to display a button to each child post.

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

This topic contains 6 replies, has 2 voices.

Last updated by manuelR-11 3 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1970501

Tell us what you are trying to do?
I have created a relationship between two custom post type "New Cars" and "Promotions" created with toolset and what I want to do is to add a button to every single element of the archive of the post type "New Cars" that links to the promotion page related to the specific element. Example: if the user clicks on the "Discover the promotion" button of the following item "Mitsubishi Space Star" he will have to land on the page of the promotion "Mitsubishi Space Star" how can I do this?

Thanks

What is the link to your site?

Archive Post Type "Auto Nuove": hidden link
Archive Post Type "Promozioni": hidden link

Thanks

#1971277

Hello, I understand you want to display a link to a related post in the results of a WordPress Archive for a custom post type. The solution depends on how your relationships are set up, and how your archive is designed. Can you tell me a bit more information?

1. What type of relationship exists between New Cars and Promotions - one-to-one, one-to-many, many-to-many, or post reference field? If it's a post reference field, which post type contains the reference field? If it's not a post reference field, I also need to know which post type is the parent and which type is the child. You can bypass the warnings when editing the relationship details to get that information.

2. Are you using the Block Editor to create this WordPress Archive, or are you using the legacy editor?

#1972033
Schermata 2021-03-02 alle 11.35.41.png

There is one to many relationship (screen in attached) and I am using Block Editor

#1972407
query-filter.png
add-fields.png

Okay it looks like you have a one-to-many (O2M) relationship between New Cars (one) and Promotions (many). So each New Car can be related to multiple Promotions. What you need to display is something like this:

(link to New Car 1)
- (link to related Promotion A)
- (link to related Promotion B)
(link to New Car 2)
- (link to related Promotion C)
- (link to related Promotion D)
- (link to related Promotion E)

...and so on for all New Cars. Is that correct? If so, the best way to do this is to create a View of related Promotions posts and place it inside the loop of the Archive for New Cars. Any time you want to display the "many" side of a relationshp, you must use a View since more than one result is possible. If you are using the Block Editor to design the New Car Archive, the problem is you cannot create a nested View inside the Archive Block Editor. The only good solution here is to create the View of related Promotions separately using the legacy Views editor, then place that View inside the Archive loop using a shortcode.

Are you familiar with the legacy Views editor? If not, you can activate it in Toolset > Settings > General: Editing Experience. Choose the option that enables both the Blocks editor and the legacy editor. Then you will find a new menu item for Views. Go to Toolset > Views and create a new View. In the first popup, choose "Display all results" and give the View a title, like "Promotions related to the current New Car".

In the View editor, choose the Promotions post type in Content Selection. Then scroll down to the Query Filter panel and add a new filter by post relationship. Choose the New Car - Promotions relationship, then choose "related to the current post in the loop" (see query-filter.png for an example).

Scroll down and click "Loop Wizard" to design the loop of this View. You can choose one of the built-in loop formats like unordered list to show a bulleted list of related posts. In the popup "Select the fields to include in the loop", Insert the post title with link or whatever information you want to display about each related Promotion.

After you have designed the loop, save the View and note the View slug at the top of the page.

Finally, edit the New Cars Archive and insert this View in the archive's loop using a custom HTML block with this code:

[wpv-view name="your-related-promotions-view-slug"]

Replace your-related-promotions-view-slug with the slug of the View you just created. Save the archive and test on the front-end of the site. You should see a bulleted list of related Promotions displayed for every New Car post in the archive.

#1973369
Schermata 2021-03-03 alle 11.03.59.png

ok understood, but i want to create a button with this solution... so in the archive page of "Auto Nuove" I would like to create a button that says "Discover the Promotion" and contain a link to a "Promotion" (Post Type) page of the current auto.

As you can see from the photo the button that says "discover the promotion I've already done" now I need to set the link in the cycle

thank you so much for the support

#1973951
1-loop-wizard.png
2-post-url.png
3-edit-loop-template.png
4-add-tb-button.png
5-button-configs.png

Are you saying the problem with this approach is you would like to display a button instead of a text link? That's no problem, you can use Views to create button(s) for the promotion link(s). One way you could do that is to use custom CSS to style the text link to look like a button. Or if you're not interested in writing CSS, you can create the button using the Toolset Button Block in your View's template. In the legacy View editor, you can use the Block Editor to design the loop template.
- In the View editor screen click "Loop Wizard" above the loop editor.
- Choose an unformatted design.
- Insert the Post URL and check the Content Template checkbox.
- Edit the template with the Block Editor.
- Insert a Button block and use the block configurations to set a dynamic source pointing at the post URL.

#1978443

My issue is resolved now. Thank you!