Skip Navigation

[Resolved] Filter a View with Multiple Relationships

This support ticket is created 3 years, 11 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by rashmiS 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1918253
Screen+Recording+2021-01-25+at+04.26.47+PM.gif
toolset_ajax_multiple_filters.png
toolset_ajax_one_filter.png

Types 3.4.1, Views 3.3.1

Hello,

I have a view (id 241) which displays Posts. I am trying to filter by related custom posts (eg, a post is related to a group; you choose a group and it filters the view). I can set this up fine with one related custom post type through the filter, but it will not allow me to do that with multiple custom post types. So I've tried following this: https://toolset.com/forums/topic/search-filter-with-two-post-relation-ships/ - basically creating a function and filter, and passing the filter in . I'm guessing I will need to do this with each custom post type (eg related people, related projects) I'd like to filter by. Again, I'm able to create one filter this way and it works fine - but as soon as I add a second filter, it stops working. The first of the two filters does not receive its options, and neither of them work to actually filter the results when 2+ custom post filters are present.

AJAX sent when selecting a group is in attached image one_filter.png. You can see the view id is being sent correctly. The id of the group chosen is 19. This works. But when I add another filter (project), it sends 0 and the filtering doesn't work (multiple_filters.png). See GIF to see options not being filled in both filters.

View setup: displays Posts, update using "AJAX results update when visitors change any filter values."
View search and pagination code:

[wpv-filter-start hide="false"]
[wpv-filter-controls]

<!-- groups -->
[wpv-control-post-relationship ancestors="group@post-group.child" url_param="wpv-group-filter"]
<div class="form-group">
<label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Groups[/wpml-string]</label>
[wpv-control-post-ancestor type="select" ancestor_type="group@post-group.child"]
</div>
[/wpv-control-post-relationship]

<!-- projects -->
[wpv-control-post-relationship ancestors="project@project-post.parent" url_param="wpv-project-filter"]
<div class="form-group">
<label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Projects[/wpml-string]</label>
[wpv-control-post-ancestor type="select" ancestor_type="project@project-post.parent"]
</div>
[/wpv-control-post-relationship]

[wpv-filter-reset output="bootstrap"]
[/wpv-filter-controls]
[wpv-filter-end]

Relevant part of functions.php:
// groups
add_filter( 'wpv_filter_query', 'filter_by_parent_group_func', 999, 3 );
function filter_by_parent_group_func( $query_args, $view_settings, $view_id ) {
if ( $view_id == 241 && isset($_GET['wpv-group-filter'])) {
if(!isset($query_args['toolset_relationships'])){
$query_args['toolset_relationships'] = array();
}
$query_args['toolset_relationships'][] = array(
'role' => 'parent',
'related_to' => $_GET['wpv-group-filter'],
'relationship' => 'post-group'
);
}
return $query_args;
}

add_filter('wpv_filter_register_url_parameters_for_posts', 'add_extra_url_param_func_group', 10, 2);
function add_extra_url_param_func_group($attributes, $view_settings){
if($view_settings['view_id'] == 241){
$attributes[] = array(
'query_type'=> 'post',
'filter_type'=> 'post_relationship',
'value'=> 'custom_field_value',
'attribute'=> 'wpv-group-filter',
'expected'=> 'number',
);

}
return $attributes;
}

// projects
add_filter( 'wpv_filter_query', 'filter_by_child_project_func', 999, 3 );
function filter_by_child_project_func( $query_args, $view_settings, $view_id ) {
if ( $view_id == 241 && isset($_GET['wpv-project-filter'])) {
if(!isset($query_args['toolset_relationships'])){
$query_args['toolset_relationships'] = array();
}
$query_args['toolset_relationships'][] = array(
'role' => 'child',
'related_to' => $_GET['wpv-project-filter'],
'relationship' => 'project-post'
);
}
return $query_args;
}

add_filter('wpv_filter_register_url_parameters_for_posts', 'add_extra_url_param_func_project', 10, 2);
function add_extra_url_param_func_project($attributes, $view_settings){
if($view_settings['view_id'] == 241){
$attributes[] = array(
'query_type'=> 'post',
'filter_type'=> 'post_relationship',
'value'=> 'custom_field_value',
'attribute'=> 'wpv-project-filter',
'expected'=> 'number',
);

}
return $attributes;
}

#1918499

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know first why you are using the outdated Toolset plugins. We always recommend running your site with the latest stable release plugin version.

*** Please make a FULL BACKUP of your database and website.***
Could you please update ALL Toolset plugins to it's latest officially released version. You can download the latest plugin release from your accounts page:
=> https://toolset.com/account/downloads/

Now, regarding the issue you reported, Toolset allows only one relationship filter at the moment. You can not filter view results with multiple relationship but as you shared another reference ticket where one of the supporter offered the workaround.

Let me have a look at your setup first and for that I will require access details.

Can you please share problem URL where you added your view and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1927895

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The thing is that you have complicated post-relationship structure where you have many-to-many and one-to-many post relationship is involved.

Toolset allows to filter by post-relationship where the parent/child select dropdowns are automatically updated when you change parent. Its just like when you select Country from the country dropdown, the next dropdown will display to associated States belongs to selected Country.

I've just added the post-relationship filter as given under:

[wpv-control-post-relationship ancestors="project@project-post.parent" url_param="wpv-project-filter"]
<div class="form-group">
	<label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Projects[/wpml-string]</label>
	[wpv-control-post-ancestor type="select" ancestor_type="project@project-post.parent"]
</div>
[/wpv-control-post-relationship]

<!-- groups -->
[wpv-control-post-relationship ancestors="group@post-group.child" url_param="wpv-group-filter"]
<div class="form-group">
	<label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Groups[/wpml-string]</label>
	[wpv-control-post-ancestor type="select" ancestor_type="group@post-group.child"]
</div>
[/wpv-control-post-relationship]


Do you see its working as expected?

#1928335

I can see it working the way you describe, but that's not the behavior I intend. I want the filters to function independently of each other, not as a parent-child relationship. Right now, if you filter a post by a related project, the groups filter only shows groups related to the project, not groups related to the original post. The latter is the intended behavior. Is this possible?

#1929035

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - there is no such option available.

#1930077

Thanks. Disappointed to find that this fairly common use case / feature is not possible in Toolset.