Skip Navigation

[Resolved] Best way to have a field contain a link to a specific post of the same CPT

This support ticket is created 5 years, 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by Beda 5 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1239297

Tell us what you are trying to do?
I need to link to a "supervisor" staff member from another staff member's single content template.
Is there any documentation that you are following?
I tried post references, and that is unsupported. I considered intermediary post types, but I'm not sure if it will work or how to add one after the fact.
Is there a similar example that we can see?

What is the link to your site?
hidden link

#1239334

It's not possible with any of the inbuilt relational mechanisms in Toolset.
You should submit a request for "unilateral relationships" explaining the precise need at this link:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

For now, since you have some form of existing (known) relation, for example, link to the other post by passing the post ID in the Link.
Assuming that you have a single field where you store the "related" SuperVisor in the current SuperVisor post, you can use that field to complete your Link.
This requires you to manually enter the ID of the related SuperVisor post in the current SuperVisor's post field.

It is currently the most dynamic way to do this, everything else would require to hardcode the link.

Hence, the steps could be:
- add a single line field to the Post Type SuperVisor
- populate each SuperVisor Post Field (single line) with the ID of the related SuperVisor Post
- create a link with the wpv-post-link shortcode passing the ID of the post to link to.
(https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153370)

Please do not forget to suggest the unilateral relations to our Product Management thru the link I shared above.

#1239612

Hi Beda,

I currently use wpvrelatedto to handle supervisor assignments using a checkbox in the custom post type. This does mean that sometimes, when the relationship isn't defined, the wpvrelatedto fails and still returns the unfiltered query.

Is there a way to have it fail more gracefully?

#1240300

Well, as I mentioned it is not possible to relate one post to another in the same type, hence querying by such data is impossible as it does not exist.

I assume you used a Checkbox as I mentioned in each post, storing a specific value as related Post?
If some of those fields are not populated then those posts later simply do not show up in a view that is filtered by the value of that field.
This is observable in 2 scenarios:
- backend filter will simply exclude all posts that have not set the particular value you pass in the filter
- front end filter will at first show all posts even if no such value is saved, and if you search by a specific value then only those are displayed.

You can avoid showing even empty posts by ordering them by that precise Custom Field. In this case, empty posts won't display on the front end search either.

But in your question, you asked how to link those posts, and that is not related to how to search for them.
If you succeeded already to connect those posts with the help provided above, if possible please start another thread if you see the custom search built for those makes trouble.

#1245287

I am using a wpvrelatedto shortcode, not a view that is related directly.

But, could I use a select list of specific CPT ids displayed as a name "firstname" + "lastname" and then the end user could select them?

#1245313

I don't understand what a wpvrelatedto ShortCode is.
Do you mean a ShortCode that displays data about Related Posts?
That is not something the user can pick or choose, but only see.

If you mean a Views Filter, in the front end, then there we have Filter so to filter by related Posts, where you can filter by one tree a time, and if the entry is not connected, filtering by related posts will remove the entry from the list, but however as you correctly observe, in case you do not filter, then it shows on the list.

This is because no filter is yet passed to the WordPress Query and hence, all the posts of that type are shown.
Since you cannot add a certain Filter more than once to a View, you cannot filter out the "unconnected" posts and later let the user filter those remaining (connected) by a specifically related post.

The only change you'd have here is to hook into the Views Query before it displays the posts and remove those which have no connection.
This could be done with https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
That filter allows you to customize the query passed to WordPress.

This custom code snippet can give a start for creating such filter:
hidden link

But, you could also simply use some HTML condition, where you'd hide the Posts in the HTML of the Views Loop if, for example, the "item" parent post ID is empty (which means it does not exist).
This does not exclude those posts without related posts from the query itself but it removes them from the produced HTML, having the same results.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/