Skip Navigation

[Resolved] Query a sub-relationship but get a single unified and ordered list

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.

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 5 months, 1 week ago.

Assigned support staff: Jamal.

Author
Posts
#2056035

I would like to be able to traverse a set of relationships so that I can display a list of Training in my Posts in an ordered way.

Posts -> Audience -> Courses -> Trainings

It is not useful to do a nested query as the Training must be ordered by delivery date so that the viewer can see upcoming training classes that relate to this post, and that it worked going forward into the future without having to associate all future trainings with more and more posts.

#2056197

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

You can build this using a few nested views, and some cleanup code. The views will return the IDs of the posts separated by comma. Then you can pass this view as a shortcode argument.
Check the custom code for cleaning up the view's markup. https://toolset.com/forums/topic/need-to-strip-views-formatting/

Note that you will need to use the legacy views to build this.

If you want to use the Blocks editor for the view, then you will need to hook into the view's query filter and programatically change the WP_Query arguments, especially 'post__in':

add_filter( 'wpv_filter_query', 'wpv_filter_query_callback', 10, 3 );
function wpv_filter_query_callback( $query_args, $view_settings, $view_id ) {
    global $post;
    if ( $view_id == 1234 ) {
        //1. get the audiences related to the current post $post->ID
        //2. get the courses from the audiances
        //3. get the trainings from the courses
        //4. $query_args['post__in'] = $trainings;
    }

    return $query_args; 
}

You should use the toolset_get_related_posts in each step to get the related posts. Check its documentation here - https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

I hope this makes sense to you. Let me know if you have any questions.