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;
}