Hi there
I have a parent EVENT post and a child DATE post in a one-to-many relationship with slug 'event-date'.
I also have a view that displays the latest child dates. The view sits in several locations around the site.
The problem I have is that if a parent EVENT post is set to draft the child DATES still appear in the view.
So I would like to add a filter to that view to only display child posts that belong to a published parent (not draft).
I tried reworking this function from another support note but I can't get it to work:
add_filter( 'wpv_filter_query', 'filter_if_parent_published', 1000 , 3 );
function filter_if_parent_published( $query_args, $view_settings ) {
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 7322) ) {
$target_post_type = "date";
$relationship_slug = "event-date";
$args = array(
'posts_per_page' => -1,
'post_type' => $target_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, 'child', 999999, 0, array(), 'post_id', 'parent' );
if(empty($get_results)) {
$query_args['post__not_in'][] = $post_array -> ID;
}
}
}
return $query_args;
}
Hoping you can help!
Regards
Rita
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Can you please share problem URL and admin access details and let me review your current structure and guide you accordingly to right direction.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
I've disable the code you added to functions.php file. You can remove it as required as its not needed.
I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link
add_filter( 'wpv_filter_query_post_process', 'func_exclude_parent_post_if_status_draft', 10, 3 );
function func_exclude_parent_post_if_status_draft( $query, $view_settings, $view_id ) {
if ($view_id==7340 and !empty( $query->posts ) ) {
$all_posts = $query->posts;
$exclude_ids = array();
foreach($query->posts as $k=>$v):
$parent_id = toolset_get_related_post($v->ID,'event-date');
$loop_post = get_post($parent_id);
if($loop_post->post_status == 'draft'){
$exclude_ids[] = $v->ID;
}
endforeach;
$filtered_posts = array_values(array_filter($all_posts, function($post) use ($exclude_ids) {
return !in_array($post->ID, $exclude_ids);
}));
$query->posts = $filtered_posts;
$query->found_posts = count($filtered_posts); // modify the count of found posts
$query->post_count = count($filtered_posts); // modify the count of displayed posts
}
return $query;
}
Can you please confirm it works as expected:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
Thank you Minesh!
That works really great.
Thank you for the adjustment to the found count as well. I didn't think of that.
Much appreciated!
Rita