Problem: I am using a View of child posts nested inside a View of parent posts to display parent and child post information. I would like to filter out parents that do not have any children.
Solution: There is no built-in way to filter out childless parent posts, so the solution requires custom code. Another ticket in the forum discusses this approach:
https://toolset.com/forums/topic/table-with-child-posts
add_filter('wpv_filter_query', 'jobs_with_bewerbungs_func', 101, 3); function jobs_with_bewerbungs_func($query, $view_settings, $view_id) { $views = array( 84 ); $relationship_slug = 'job-bewerbung-multis'; $parent_type_slug = 'job'; // you should not edit anything below this line if ( in_array( $view_id, $views ) ) { $ids = array(); $parents_args = array( 'post_type' => $parent_type_slug, 'numberposts' => -1, ); $parents = get_posts( $parents_args ); foreach($parents as $parent) { $children = toolset_get_related_posts( $parent->ID, $relationship_slug, 'parent', 1000000, 0, array(), 'post_id', 'child' ); if( !is_array($children) || count($children) < 1 ) { array_push( $ids, $parent->ID ); } } $query['post__not_in'] = isset($query['post__not_in']) ? $query['post__not_in'] : array(); $query['post__not_in'] = array_merge($query['post__not_in'], $ids ); } return $query; }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 5 years, 12 months ago.
Assisted by: Christian Cox.