[Resolved] Two relationships in view using WP_Query and wpv_filter_query
This thread is resolved. Here is a description of the problem and solution.
Problem:
The issue here is that the user wants to filter the current view by 2 post relationships from different post types.
Solution:
This can be resolved by using the code below.
//Return only posts from the current author when listing posts of type company:
function tssupp_filter_query( $query_args, $view_settings, $view_id ) {
if($view_id == 123){
$query_args['toolset_relationships'] = array(
array(
'role' => 'child',
'related_to' => 63,
'relationship' => 'leverancier-categorie-leverancier'
),
array(
'role' => 'child',
'related_to' => 51,
'relationship' => 'vestiging-leverancier'
)
);
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 99, 3 );
This can be added to the Toolset Custom code section in Toolset->Settings->Custom Code. Change the 123 to the ID of your view and adjust the relationship slug and related_to to the correct ID of the posts.
I am trying to use this code to display items that have a relationship with two post types, in this case I want the display all 'leveranciers' that are related to post type 'vestiging' with post_id 50 and 'leverancier-cate' with post_id 63.
But the post array always returns nothing. I know it is a complicated issue, but I hope you can help me. If you need any other information I would be happy to supply it.
With this you will be able return all the ID's of the posts in the different relationships, then you can perform an array intersect to return the items that are the same in both arrays. hidden link
Your website is a little tricky to navigate, however I spot an issue. It seems that you are using the incorrect ID to get the posts in the relationship.
If you want to return the posts of the "vestiging" post type you will need to use the ID of the related "leveranciers". From what I checked on the backend you were using the ID of a "leveranciers".
Maybe I should explain again what I am trying to do again?
I have 3 post types, establishments (vestiging), supplier (leverancier) and supplier categories (leverancier-cate) now a user has selected both an establishment and a supplier category he now arrives at a page with an URL containing both parent post ids in this case 50 (the parent establishments) and 63 (the parent supplier categories). Now I want to display a view with suppliers that contain both parents.
I hope this paints a better picture of what I am looking for.