Home › Toolset Professional Support › [Resolved] View with child post type: hide posts where parent is private/concept (part 3)
Problem: I have a post relationship between two post types. I want to show a View of child posts where the parent post is not private.
Solution: Use custom code to test each child post's parent. Private parent posts will not be returned for any guest Users. Also test the post status, so logged-in Users will not see child posts of their own private parent posts.
add_filter('wpv_filter_query', 'ts_parent_not_private_func', 10, 3); function ts_parent_not_private_func($query, $view_settings, $view_id) { $views = array( 16813, 16403 ); // only filter these views if( in_array( $view_id, $views ) ) { $child_ids = array(); // we will push IDs here if the parent is not found, or private $children_args = array( 'post_type' => 'training-datum', 'posts_per_page' => '-1', 'post_status' => 'publish', // you may want to add more filters here to improve the performance of this query ); $children = new WP_Query($children_args); foreach( $children->posts as $child ){ $parent = toolset_get_related_post( $child, 'training_training-datum', 'parent' ); // if there is no parent or the parent is private, push this ID into the exclusion array if( !$parent || get_post_status($parent) == 'private' ){ array_push( $child_ids, $child->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'], $child_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_post
https://codex.wordpress.org/Class_Reference/WP_Query
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)
Related documentation:
This topic contains 5 replies, has 2 voices.
Last updated by martijnH 6 years, 1 month ago.
Assisted by: Christian Cox.
Sorry, my ticket https://toolset.com/forums/topic/view-with-child-post-type-hide-posts-where-parent-is-private-concept-2/ was not resolved.
I was logged in and eveything looked right. The childs with private parents were not shown. But when I logged out, the childs with private parent showed up again in the view.
See screenshots and hidden link
I see, I overlooked the fact that guests cannot see private posts so the related post queries based on the parent post are failing. We can take a different approach that tests whether or not a parent post is found for each child, as well as the parent post status (if it exists). This should fix the problem for all Users, assuming that you do not want to show any child posts that have no parent.
add_filter('wpv_filter_query', 'ts_parent_not_private_func', 10, 3); function ts_parent_not_private_func($query, $view_settings, $view_id) { $views = array( 16813, 16403 ); // only filter these views if( in_array( $view_id, $views ) ) { $child_ids = array(); // we will push IDs here if the parent is not found, or private $children_args = array( 'post_type' => 'training-datum', 'posts_per_page' => '-1', 'post_status' => 'publish', // you may want to add more filters here to improve the performance of this query ); $children = new WP_Query($children_args); foreach( $children->posts as $child ){ $parent = toolset_get_related_post( $child, 'training_training-datum', 'parent' ); // if there is no parent or the parent is private, push this ID into the exclusion array if( !$parent || get_post_status($parent) == 'private' ){ array_push( $child_ids, $child->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'], $child_ids ); } return $query; }
Check the post type slug "training-datum", I'm assuming this is correct but you may need to change it.
Sorry to say this does not work for me. I still have the same result.
Just to be sure: In the yellow resolution box on https://toolset.com/forums/topic/view-with-child-post-type-hide-posts-where-parent-is-private-concept-2/ you write "...so that the View only shows children of parents that are marked private"
This should be "... so that the View only shows children of parents that are NOT marked private". Do not know this makes a difference for the solution.
May I log in to see why this isn't working as expected? Please provide login credentials in the private fields here and I'll take a closer look.
Okay check now, I made a transposition error here:
'posts_per_page' => '1',
That should have been '-1' to get all the posts. I made that change in the functions.php file for you so be sure to pull that down to your local repository.
My issue is resolved now. Thank you!