Skip Navigation

[Resolved] Filter a parametric search query by a related post's custom field values.

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Luo Yang 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2408001

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.

#2408321

Hello,

Since it is a custom codes issue, please share a test site with the same problem, also point out:
1) the problem page URL, where I can test the results in frontend
2) Where I can edit your custom PHP codes,

I need a live website to test and debug it

#2410659

Thanks for the details, I assume we are talking about the page of your website:
hidden link
I have done below modifications in your website:
Edit the custom codes "listing-filter-location-type", replace line from:
function listing_location_type_func_func($query, $view_settings, $view_id) {

With:
function listing_location_type_func($query, $view_settings, $view_id) {

Please test again, check if it is fixed.

More help:
https://developer.wordpress.org/reference/functions/add_filter/