Skip Navigation

[Gelöst] Filtering View Based on Relationship with Two Parents

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 6 Jahren, 5 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 15 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Beda vor 6 Jahren, 1 Monat.

Assistiert von: Christian Cox.