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/
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:
versteckter Link
Custom codes:
versteckter Link
Result page:
versteckter Link
It works!!! Thank you so much!