Skip Navigation

[Resolved] is it possible toolset_get_related_posts orderby meta_value in intermediate post

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

This support ticket is created 4 years, 4 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by markW-21 4 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1776359

Tell us what you are trying to do?
Get children ordered by the value from intermediate post

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1776807

Hello,

Yes, it is possible, see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Function toolset_get_related_posts() has a parameter "orderby_role":
string orderby_role: Name of the role by which ordering should happen. If no value is provided, the first value from the 'role_to_return' argument will be used.

#1776895

ok, I've got it working, however not for secondary language using wpml.

$result = toolset_get_related_posts($series_id, 
                                        $relationship_slug, 
                                        array(
        'query_by_role' => 'parent', // origin post role
        'role_to_return' => 'child', // role of posts to return
        'return' => 'post_object', // return array of IDs (post_id) or post objects (post_object)
        'limit' => 999, // max number of results
        'offset' => 0, // starting from
        'args' => array(
            'meta_key'      => 'wpcf-priority',
            'meta_value'    => '%',
            'meta_compare'  => 'LIKE',
        ), // Additional query arguments
        'orderby' => 'meta_value_num',
        'orderby_role' => 'intermediary',
        'order' => 'ASC',
        'need_found_rows' => false, // also return count of results
    ));
#1777045

I assume you are going to order the result by a custom field "wpcf-priority" of intermediate post type.

I have tried it in my localhost, in your case, you need to do these:
1) Translate all three post types: parent, child and intermediate post types

2) Setup your PHP codes to query the intermediate posts, for example:
'role_to_return' => 'intermediary', // role of posts to return

3) in the loop of variable $result, get the child post ID by intermediary post ID
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
then use intermediary post ID to display the child post information

#1777065

I did it in a different way
1. I get children without sorting
2. Get intermediary post and meta_data for each one, create a dictionary myDict[meta_data] = child
3. ksort(myDict)
Of cause meta-data wpcf-priority must be uniq which is a case for me.

Thanks anyway.

#1778081

Yes, it is possible with your way, does it work for you? if not, have tried the solution I mentioned above?
https://toolset.com/forums/topic/is-it-possible-toolset_get_related_posts-orderby-meta_value-in-intermediate-post/#post-1777045
Please let me know if you need more assistance for it, thanks

#1778381

My issue is resolved now. Thank you!