Tell us what you are trying to do?
We have the following post types: Listing (requirement-listing), Property (property), Owner Profile (owner-profile)
We have the following relationships:
1) Listing - one-to-one - Property
2) Owner Profile - one to many - Listings
We have a View (ID 1238) that must show Listings. We want to filter this view with (multiple) custom field values from the related Property and related Owner Profile. (As you can see, each Listing has only one parent Owner Profile, and one child Property).
We have the following toolset custom fields to filter from the related property: location_type (radio), property_type (radio)
We have the following toolset custom fields to filter from the related owner-profile: verified (checkbox), review_score (number)
We have set up the view to have the search filters in place, but obviously they wouldn't work out of the box.
So we've pieced together some custom code but unfortunately, it returns 0 posts, regardless of the value chosen for the custom field in the search filter.
I'm sure we are almost there - I think I'm missing something very obvious.
Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts-legacy
https://toolset.com/forums/topic/only-display-parent-posts-that-have-children-with-a-custom-field-value/page/2/
Here is what we have tried:
add_filter('wpv_filter_query', 'listing_location_type_func', 101, 3);
function listing_location_type_func_func($query, $view_settings, $view_id) {
$views = array( 1238 );
$relationship_slug = 'listing-property';
$parent_type_slug = 'requirement-listing';
// you should not edit anything below this line
if ( in_array( $view_id, $views ) and isset($_GET['wpv-wpcf-location-type']) and $_GET['wpv-wpcf-location-type']!="" ) {
$ids = array();
$parents_args = array(
'post_type' => $parent_type_slug,
'numberposts' => -1,
);
$parents = get_posts( $parents_args );
//echo print_r($parents);
foreach($parents as $parent) {
$children = toolset_get_related_posts(
$parent->ID,
$relationship_slug,
'parent',
1000000,
0,
array('meta_key' => 'wpcf-location-type',
'meta_compare' => '=',
'meta_value' => $_GET['wpv-wpcf-location-type']),
'post_id',
'other'
);
$children = $query->posts;
//echo print_r($children);
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 );
//$query['post__in'] = isset($query['post__in']) ? $query['post__in'] : array();
//$query['post__in'] = array_merge($query['post__in'], $ids );
//$query['meta_query'] = 0;
}
return $query;
}
What is the link to your site?
The site is under development, but I can share credentials with you if needed.