Hi there
I have a very nice filter in place, kindly submitted by one of the support team on another support post, to filter views results to only display posts that have children. However, I would like to see if its possible to tweak this filter to add an additional condition to only display posts that have children AND if any of those children have a certain custom field value.
In this case, we have parent 'events' and child 'tickets'.
Child 'tickets' have a custom select field with values of 1, 2, 3 or 4.
I would like the view to ONLY display the parent 'event' post if:
1. It has child 'tickets'
2. Any one of those child 'ticket' posts has a custom field value of 1.
Here is the filter I currently have in functions.php:
add_filter('wpv_filter_query', 'events_with_tickets_func', 101, 3);
function events_with_tickets_func($query, $view_settings, $view_id) {
$views = array( 1075, 15777, 1147, 167 );
$relationship_slug = 'event-ticket';
$parent_type_slug = 'event';
// 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;
}
Do you think it is possible?
Hoping you can help and thank you in advance!
Regards
Rita