Skip Navigation

[Resolved] sort by parent post title

This support ticket is created 4 years, 8 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 1 reply, has 2 voices.

Last updated by Beda 4 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#1542995

Hi,

I have added custom columns in admin section and now want to add sorting on those columns based on relationship. for example I have made a relationship between kb-articles and products and added products title as custom column in kb article view. Now I want to sort them using product title, please let me know what should I add in meta query to sort them as now toolset updated the post-relationship in different tables. Here are some screenshots as well:

hidden link
hidden link

Also for showing the product title, I am using this function toolset_get_parent_post_by_type( $post_id, 'product' ); but its giving on single product id, instead of all selected product. Please help me with this as well.

#1543033

Toolset doesn't support any backend admin posts list sorting for related posts.

This is custom code that you will need to implement, and it will not be possible with the current setup, even with custom code.

To get the data from the post, you would need to have each post ID in the list.
So if your list lists lets say native posts related to any other post, you would need to ensure you have the IDs of those native posts, which are you listing now.

Then, you would need to populate your custom code which adds the column of related posts.
But that can ONLY work if you are in a One To One, or one To Many relationships, and you are listing the MANY ends so that the custom column will show ONLY ONE related post each post in the list.

Otherwise, no sorting can ever sort properly as it would sort by more than one item
Does this make sense?
Hence with your current relationship, you can't do this, not even with custom code, because you would be sorting more than one item each item in the list, and there is simply no way WordPress will "understand" that.

You'd need to (maybe) get the FIRST of the related posts, and order the custom column by that, but that would then make it only sortable by the FIRST related post each post in the list.

I suggest submitting a request to have the opportunity to list related posts in that admin column, here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
But remember this will only be possible ever with a One to One or One to Many, not a Many to Many relationships.
Submitting the request will allow the Product Management to evaluate future implementations.

As for toolset_get_parent_post_by_type, this method is elaborated here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type and it states:

Retrieves an ID of the parent post, using a legacy post relationship. Or, if new post relationships are already enabled, relationships migrated from the legacy implementation. For this to work, there needs to be a relationship between $target_type and the provided post’s type. Note: For more complex cases, use toolset_get_related_post() or toolset_get_related_posts() functions.

It is for retrieving ONE parent post, so - in a One To Many Relation or One To One, but not a Many To Many, where you will always have potentially MANY related posts.
For getting posts in a many to many you'd use https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts, but that gives you not only ONE result, instead, all related posts.