Hello,
I have a view showing all child posts with 2 filters, one for each parent post.
Here is the code I set up :
[wpv-filter-controls]
[wpv-control-post-relationship ancestors="produit" url_param="wpv-produit-filter"]
<div class="form-group">
<label>[wpml-string context="wpv-views"]Produits[/wpml-string]</label>
[wpv-control-post-ancestor type="multi-select" ancestor_type="produit"]
</div>
[/wpv-control-post-relationship]
[wpv-control-post-relationship ancestors="fournisseur" url_param="wpv-fournisseur-filter"]
<div class="form-group">
<label>[wpml-string context="wpv-views"]Fournisseurs[/wpml-string]</label>
[wpv-control-post-ancestor type="multi-select" ancestor_type="fournisseur"]
</div>
[/wpv-control-post-relationship]
[/wpv-filter-controls]
But in the filter section, I can't find how to add the second post relationship filter...
Thank you.
Dear roman,
This is expected result, you an setup only one post type relationship filter in a single view, and here is a workaround with some custom codes using Views filter hook wpv_filter_query, for example:
1) edit your view, add a " Post relationship filter":
Select posts that are children of the Post with ID set by the URL parameter wpv-produit-filter.
eg. hidden link
2) Use filter hook wpv_filter_query to apply another filter, add below codes into your theme/functions.php:
add_filter( 'wpv_filter_query', 'filter_by_parent_fournisseur_func', 999, 3 );
function filter_by_parent_fournisseur_func( $query_args, $view_settings, $view_id ) {
if ( $view_id == 99 && isset($_GET['wpv-fournisseur-filter'])) {
$query_args['meta_query'][] = array(
'key' => '_wpcf_belongs_fournisseur_id',
'value' => $_GET['wpv-fournisseur-filter'],
);
}
return $query_args;
}
add_filter('wpv_filter_register_url_parameters_for_posts', 'add_extra_url_param_func', 10, 2);
function add_extra_url_param_func($attributes, $view_settings){
if($view_settings['view_id'] == 99){
$attributes[] = array(
'query_type'=> 'posts',
'filter_type'=> 'post_relationship',
'value'=> 'custom_field_value',
'attribute'=> 'wpv-fournisseur-filter',
'expected'=> 'number',
);
}
return $attributes;
}
Please replace 99 with your view's ID
That is just GREAT.
Thank you very much !