Skip Navigation

[Closed] Add a link to a post title when using a many to many relationship.

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

Author
Posts
#1896689

JSG

I have created a searchable view based on a many to many relationship. The search works well. Now I want users to be able to click on a result and have it take them to a full job listing which is built by combining the two custom post types used in the relationship.

Post Types are Job Details and Job location.

A job listing will be those two post types combined.

Here is a link to a test page.
hidden link

The questions are:

Should a view be created to display the combined data or a content template?

And

How is the searchable view coded to point the link properly?

#1896815

There are several viable options, but it seems that the ideal solution is to expose the intermediary post type so that it is visible in wp-admin and each intermediary post has a unique front-end URL. Then you would display a link to the intermediary post in the results of your custom search View. You can apply a Content Template to the entire intermediary post type in wp-admin, so the same design is applied to all the intermediary posts on the front-end of the site. In this Content Template, you can display information from both of the related post types in the M2M relationship, as well as any information stored in relationship fields in the intermediary post type (if those exist). So after searching, the User will click the link to the deisred intermediary post in the search results, and will be redirected to that intermediary post's single post permalink (URL). There they will find data about the parent Job Details and parent Job Location.

Would that scenario work for your case?

#1897655

JSG

This is a reasonable solution. Let me build it and see how it goes.

Please leave this ticket open for a bit and I will feed back my success.

#1897663

JSG

Is it possible to change the slug and page name for the intermediary post type in the custom template design

In my case I'd like to change it to Private Basketball Coach - Personal Trainer - Nashville

(job details, job-title + Job location, city)

This will help with SEO and make it nicer to look at.

#1897707
rewrite.png

Is it possible to change the slug and page name for the intermediary post type in the custom template design
It's possible to customize the URL slug of the post type and it's possible to give the post itself a more SEO-friendly title/slug, but not by simply editing a Content Template. The Content Template only affects the visual design of the page, not the permalink structure or slugs involved here.

It is not possible to alter the post type slug of an existing intermediary post type, for technical reasons. Instead, you can adjust the intermediary post type's rewrite option to customize the URL without modifying the actual post type slug. See the attachment rewrite.png, showing how you would provide a customized slug in the URL instead of actually modifying the post type slug. Go to Toolset > Post Types, then edit the intermediary post type to see this Options panel.

As far as the post title and post slug, those are arbitrary and can be adjusted in wp-admin when editing the intermediary post directly. If the post is edited using the classic editor, you can manage the title and slug at the top of the page. If the post is edited using the Block Editor, the slug can be adjusted in the main block editor sidebar, inside the Permalink panel. Custom code is required to create automatic, customized intermediary post titles using components of each related post title.

#1898001

JSG
2021-01-07 15_20_26-Window.jpg
2021-01-07 15_20_43-Window.jpg
2021-01-07 15_20_57-Window.jpg

I was successful in using the post rewrite function and building a custom template that works well.
hidden link

I also have my search view
hidden link

But I'm not sure how to link the results to the intermediary post it references to.

I have uploaded images of the code used in the view.

What would I need to edit to get the link to work?

#1900151
Screen Shot 2021-01-10 at 9.15.14 AM.png

In the loop template "Loop item in Search City by Job", you can insert a link to the intermediary post using the Post Title with Link shortcode. Click the "Fields and Views" button above the editor area, then click "Post title with link". The main thing to note here is that since 3 different post types are involved in this View, you may need to use the Post Selection tab on the left side of the popup to indicate which post's link you want to display. Whether or not you need to use this tab depends on the Content Selection settings for the View. Only one post type should be selected there. If the View is configured to search and display the intermediary post type, then there is nothing else you need to do. The post title with link shortcode will default to the post type being displayed in the results, so the context is already set here.

On the other hand, if the View is configured to search and display one of the two other post types in the M2M relationship, then you'll need to use the Post Selection tab in the popup to tell the system which related post's link should be displayed in the results. I'll assume that the View is configured to display one of the two related post types, not the intermediary post type.

After you click "Post title with link" in the popup, you must click the "Post selection" tab on the left sidebar to choose the intermediary post type in this M2M relationship (see the attachment). These configurations should produce a link shortcode in the following format:

[wpv-post-link item="@job-location-job-detail.intermediary"]

If the View is configured to search and display the intermediary post type in Content Selection, the item attribute is not required in the shortcode. In this case, the shortcode would be simpler:

[wpv-post-link]

If you want to create a custom HTML link tag, you could use the wpv-post-url shortcode instead of wpv-post-link. That will output only the URL of the intermediary post without the link tag. You could create your own custom HTML link tag and place the wpv-post-url shortcode inside its href attribute like this:

<a href="[wpv-post-url item='@job-location-job-detail.intermediary']">Your custom link text</a>

Notice how single and double quotation marks are alternated in nested attribute structures.

View shortcodes documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-link
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-url

The topic ‘[Closed] Add a link to a post title when using a many to many relationship.’ is closed to new replies.