Skip Navigation

[Resolved] Filtering View Based on Relationship with Two Parents

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to filter a View using two one-to-many relationships, but I am only able to add one relationship filter using the GUI. I would like to be able to pass both parent post filter values into the View using shortcode attributes.

Solution: Use the wpv_filter_query API to apply multiple toolset relationships filters, and use any arbitrary shortcode attributes to pass values into the View.

add_filter( 'wpv_filter_query', 'query_two_rels_by_shortcode_atts',99,3 );
function query_two_rels_by_shortcode_atts( $query_args, $views_settings, $view_id) {
  global $WP_Views;
  $view_ids = array( 498 );
  
  if (in_array($view_id, $view_ids)){
    $shortcode_atts = array('childid'=>0, 'courseid'=>0);
    $viewSlug = get_post_field('post_name', $view_id);
    foreach($WP_Views->view_shortcode_attributes as $atts) {
      if($atts['name'] == $viewSlug) {
        $shortcode_atts = $atts;
        break;
      }
    }
    $child_id = $shortcode_atts['childid'];
    $course_id = $shortcode_atts['courseid'];
    $query_args['toolset_relationships'] = array(
      array(
        'role' => 'child',
        'related_to' => $child_id,
        'relationship' => 'household-child_household-enrollment',
      ),
      array(
        'role' => 'child',
        'related_to' => $course_id,
        'relationship' => 'household-course_household-enrollment',
      ),
    );
  }
  return $query_args;
}

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 10 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
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)

This topic contains 15 replies, has 3 voices.

Last updated by Beda 5 years, 6 months ago.

Assisted by: Christian Cox.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.