Skip Navigation

[Resolved] Displaying only posts in a relationship

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

Last updated by zoeS 1 year, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#2534851

Tell us what you are trying to do?
I have two post types: therapists and therapies, in a many to many relationship. I would like my view to display only those therapies that have a therapist. I think I remember in the old system doing this by querying the intermediate post type, but can't see how to do it now.

Is there any documentation that you are following?
This looks like the same question, but the code doesn't seem to work for me. I always get the full list of therapies, some of which don't have a practitioner: https://toolset.com/forums/topic/create-view-to-display-post-with-relationship/

#2535165

Hello,

In the post type relationships created with latest version Toolset Types plugin, you need to follow our document to setup the custom PHP codes:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example, you can modify the PHP codes as below:

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_query', 'filter_agents_without_review_fn', 1000 , 3 );
function filter_agents_without_review_fn( $query_args, $view_settings, $view_id ) {
 
    if ( $view_id == 16 ) {
 
        $post_type = "therapist";
        $relationship_slug = "therapist-therapy";
 
        $args = array(
            'posts_per_page'   => -1,
            'post_type'        => $post_type,
            'post_status'      => 'publish',
        );
 
        $posts_array = get_posts( $args );
 
        foreach ($posts_array as $post_array ) {
 
            $get_results = toolset_get_related_posts( 
              $post_array->ID, 
              $relationship_slug, 
              array(
                'query_by_role' => 'parent',
                'role_to_return' => 'child',
                'return' => 'post_id'
              )
            );
            if(empty($get_results)) {
                $query_args['post__not_in'][] = $post_array -> ID;
            }
        }
     
    }
 
    return $query_args;
}

I have tried it in a fresh WP installation, it works fine. See below test site:
Login URL:
hidden link

Custom codes:
hidden link

Result page:
hidden link

#2535189

It works!!! Thank you so much!