Skip Navigation

[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.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

This support ticket is created 3 years, 10 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 9 replies, has 2 voices.

Last updated by wesselK 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1962529

Tell us what you are trying to do?

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.

function tssupp_filter_query( $view_args, $view_settings, $view_id )  {
if($view_id == 123){
$query = new WP_Query( 
array(
'post_type' => 'leverancier',
'posts_per_page' => -1,
'toolset_relationships' => array(
array(
'role' => 'child',
'related_to' => 50,
'relationship' => array( 'vestiging', 'leverancier' )
),
array(
'role' => 'child',
'related_to' => 63,
'relationship' => array( 'leverancier-cate', 'leverancier' )
)
),
'meta_key' => 'wpcf-genre',
'orderby' => 'meta_value',
'order' => 'ASC',
)
);
$posts = $query->posts;
var_dump($posts);
}
      
    return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );

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.

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

Is there a similar example that we can see?
n / a

What is the link to your site?
hidden link

#1962899

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Wessel,

Thank you for getting in touch.

I suggest that you use the function below to get the related posts information.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

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

Finally you can run the WP_Query with the 'post__in' attribute to get the post objects from the relationships.
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
Please let me know if this points you in the right direction.

Thanks,
Shane

#1965213

I tried the follow code:

function tssupp_filter_query( $view_args, $view_settings, $view_id ) {
if($view_id == 123){
$vestiging = toolset_get_related_post( 50, array( 'vestiging', 'leverancier' ) );
$cate = toolset_get_related_post( 63, array( 'leverancier-cate', 'leverancier' ) );

var_dump($vestiging);
var_dump($cate);
}

return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );

But it returns this:
int(0) int(0)

Even when there is a relationship between vestiging(50) and multiple 'leveranciers'

#1965545

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Wessel,

Would you mind allowing me to have admin access to the site so that I can have a look at this for you ?

I've enabled the private fields for your next response. Also let me know the page that you are testing this on.

Thanks,
Shane

#1968041

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Wessel,

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".

This should now produce the desired results.

Thanks,
Shane

#1970363

I am sorry, but I think I just don't fully understand your response whatever I tried to do I always get the same return of 0. I have tried:

$leve = toolset_get_related_post( 127, array( 'vestiging', 'leverancier' ) );
$leve = toolset_get_related_post( 50, array( 'leverancier', 'vestiging' ) );
$leve = toolset_get_related_post( 127, array( 'vestiging', 'leverancier' ) );
$leve = toolset_get_related_post( 50, array( 'leverancier', 'vestiging' ) );

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.

#1971275

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Wessel,

I understand what you are trying to achieve however would you mind allowing me to grab a copy of the site so that I can test here locally ?

Secondly I will need the copy to provide to our 2nd tier supporters for further assistance on debugging to see why the query isn't working.

Please let me know and I can go ahead and grab the copy of the site.

Thanks,
Shane

#1971867

Yes of course, go ahead. And I already want to thank you for all the assistant you have been providing me.

#1972437

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Wessel,
Here is the final 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 );

I've tested this on my end and it indeed provides the correct result. The expected output of your view is now "Beveiliging 1"

Please add this code to your site and let me know if it works for you.

Thanks,
Shane

#1973205

My issue is resolved now. Thank you!