Tell us what you are trying to do?
I have the following post types:
- customers (kunden) -> slug: kunden
- projects -> slug: projekt
The Post Types are in a one to many relationship. One customer can have several projects.
I have created a view (id: 1982 ) that shows all customers and in this view
I have another view (id: 1860) that shows all projects
=> with the Query Filter: Select posts in a Kunden Projekte relationship that are a related to the current post in the loop.
At the moment I can see all customers, regardless of whether a project is assigned to them or not (see picture)
I would now like to create a view that only shows customers to whom a project is assigned. How can I achieve this?
I've tried the following code, but I don't get any results.
add_filter('wpv_filter_query', 'func_filter_post_that_are_related', 10, 3);
function func_filter_post_that_are_related($query, $view_settings, $view_id) {
global $post;
$views = array( 1982 ); // only filter these views
if( in_array( $view_id, $views ) ) {
$include_ids = array(); // push all related project IDs here
$projekt_args = array(
'post_type' => 'projekt',
'posts_per_page' => -1,
);
$projekte = new WP_Query($projekt_args); // get all clients
foreach ($projekte->posts as $projekt ) {
$kunden = toolset_get_related_posts(
$projekt->ID,
'kunde-projekt',
'parent',
1000000,
0,
null,
'post_id',
'child'
);
if( ! empty($projekt) ){
$include_ids[] = $projekt->ID; // push the related project IDs into this array
}
}
$query['post__in'] = isset( $query['post__in'] ) ? $query['post__in'] : array();
$query['post__in'] = array_merge($query['post__in'], $include_ids ); // update the main query to exclude related project ids
}
return $query;
}
I'm grateful for any help 😉
Is there any documentation that you are following?
https://toolset.com/forums/topic/views-within-a-view/#post-1784287
Is there a similar example that we can see?
What is the link to your site?
hidden link