Skip Navigation

[Resolved] Filtering View with two complex Relationships – get_related_posts() error

This thread is resolved. Here is a description of the problem and solution.

Problem:

Add a filter with hook wpv_filter_query.

Solution:

You can follow our document to setup the PHP codes.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 4 years, 7 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 2 voices.

Last updated by georgeK-5 4 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1656379
Error_on get_related_posts.png

This is a continue fro the ticket here https://toolset.com/forums/topic/filtering-view-with-two-complex-relationships/#post-1642687

I tried your solution, (which is what I want), but I get the below error. The credentials were sent on the previous ticket.

Fatal error: Uncaught Exception: All provided arguments for a related element must must have a role name as the array key.
in /var/www/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php on line 220

The code I use is this

function filter_by_parent_sea_service_vessel_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 456 && isset($_GET['wpv-vessel-filter'])) {
     	//get related "Seaman Sea Services" posts IDs
		 
		// Query "Seaman Sea Services" posts IDs from vesselID		
		$vessel_parent_id = $_GET['wpv-vessel-filter'];
		$vessel_sea_service_relationship_slug = 'sea-service-vessel';
      
	     error_log('vessel_parent_id: ' . $vessel_parent_id );  
      
		$posts_seaman_sea_services = toolset_get_related_posts(
			// post ID to which the results should be related.
			$vessel_parent_id, 
			// Relationship slug.
			$vessel_sea_service_relationship_slug,
          	//array( 'vessel', 'seaman-sea-service' ), 
			// Additional arguments.
			array (
				// Specify the role to query by and to return. 
				// Notice that we can use "other" in this situation.
				'query_by_role' => 'parent',
          		'return' => 'post_id',              
				'role_to_return' => 'other'
			)
		);
      
   // $args = array(
   //     'post_type'  => 'seaman-sea-service',
   //     'meta_query' => array(
   //         array(
   //             'key'     => 'wpcf-sea-service-vessel',
   //             'value'   => '56230',
   //             'compare' => '=',
   //         ),
   //     ),
   // );
        
		if (empty($posts_seaman_sea_services)) {
			$posts_seaman_sea_services = array('0');
        } else {
            $seamen_sea_service_posts_ID = array_keys($posts_seaman_sea_services);         
            //$seamen_sea_service_posts_ID = wp_list_pluck( $posts_seaman_sea_services, 'ID' );

            //Query related Seaman
            $seaman_parent_id = $posts_seaman_sea_services;
            $seaman_relationship_slug = 'seaman-seaman-sea-service';

            $seamen_posts = toolset_get_related_posts(
                    $seaman_parent_id,
                    $seaman_relationship_slug,
                    array(
                            'limit'         => 999,
                            'return'        => 'post_id',
                            'role_to_return'    => 'parent'
                    )
            );	

            $seamen_posts_ID = array_keys( $seamen_posts );
        }     
		//error_log('seamen_posts_ID: ' . $seamen_posts_ID );
      	//error_log('seaman-sea-services: ' . $seamen_sea_service_posts_ID );
      	//error_log('seaman-sea-services: ' . array_keys($posts_seaman_sea_services) );
      
      	if (empty($seamen_posts_ID)) {
			$seamen_posts_ID = array('0');
            $query_args = array( 'post_type' => 'seaman', 'post__in' => $seamen_posts_ID ); 
        } else {
            $query_args = array( 'post_type' => 'seaman', 'post__in' => $seamen_posts_ID ); 
        } 
        //query_args['post__not_in'] = null;
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'filter_by_parent_sea_service_vessel_func', 999, 3 );

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'] == 1111456){
		$attributes[] = array(
		'query_type'=> 'posts',
		'filter_type'=> 'post_relationship',
		'value'=> 'custom_field_value',
		'attribute'=> 'wpv-vessel-filter',
		'expected'=> 'number',
		);
	}
	return $attributes;
}
#1656635

I apologize for sending again, but I need to close issue this asap!

#1656637

Hello,

I can get your website credentials in previous thread.

Since it is a custom codes problem, please provide below details:
- Where I can edit your custom PHP codes
- Are we talking about the same problem page:
hidden link
and view:
hidden link

#1656653

Hello Luo, I am very glad that you answered so quick.

Yes, it is the same problem page with the URL parameter and view.
The custom code can bee edited in
hidden link
Toolset - > Settings -> Custom Code

Custom code Snippet "vsl-custom"

The administrator credentials for the site are on the previous thread.

Please let me know if you need anything else.

#1656701

Thanks for the details, I have changed the PHP codes as below:

add_filter( 'wpv_filter_query', function( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 456 && isset($_GET['wpv-vessel-filter'])) {

      	// Query "Seaman Sea Services" posts IDs from vesselID		
		$vessel_parent_id = $_GET['wpv-vessel-filter'];
      	$vessel_sea_service_relationship_slug = 'vessel-seaman-sea-service';
       
        $posts_seaman_sea_services = toolset_get_related_posts(
            // post ID to which the results should be related.
            $vessel_parent_id, 
            // Relationship slug.
            $vessel_sea_service_relationship_slug,
            // Additional arguments.
            array (
                // Specify the role to query by and to return. 
                // Notice that we can use "other" in this situation.
                'query_by_role' => 'parent',
                'return' => 'post_id',              
                'role_to_return' => 'child'
            )
        );
      	$post_ids = array(0);
        foreach($posts_seaman_sea_services as $key=> $value){
        	$posts_seaman = toolset_get_related_posts(
            // post ID to which the results should be related.
                $value, 
                // Relationship slug.
                'seaman-seaman-sea-service',
                // Additional arguments.
                array (
                    // Specify the role to query by and to return. 
                    // Notice that we can use "other" in this situation.
                    'query_by_role' => 'child',
                    'return' => 'post_id',              
                    'role_to_return' => 'parent'
                )
            );
          	$post_ids = $posts_seaman[0];
        }
      	$query_args['post__in'] = $post_ids;
    }
    return $query_args;
}, 10, 3 );

And I have tried to create a new "vessel" post, and test it in front-end:
hidden link

It works fine.

Please test again, check if it is fixed

#1656723
3.Search_For_Seaman_that_have_the_Vessel_selected_in_the_sea_service.png
2.Search_For_Vessel.png
1.Add_sea_Service.png
0.This_is_the_field_that_is filled in the sea service form.png

I think this is the wrong relation slug.

The association of the post type "vessel" and post type "seaman-sea-service" is done through the post reference field "sea-service-vessel". The link is added to the add Sea Service form "see images attached.

The front-end user will select only from existing Vessels. He will not be able to add Vessels posts.

#1657765

Thanks for the details, I have edited above PHP codes, this line, from:
$vessel_sea_service_relationship_slug = 'vessel-seaman-sea-service';

To:
$vessel_sea_service_relationship_slug = 'sea-service-vessel';

Test it in front-end, for example:
hidden link

It works fine.

#1659031

My issue is resolved now. Thank you!

#1659163

The code needed a small change to fill all the returned posts into the array in order to search with.

          	//$post_ids = $posts_seaman[0];
          	array_push($post_ids, $posts_seaman[0]);
#1659439

You are right, it need to push new seaman ID into array, thanks for sharing the solution.

#1662717

My issue is resolved now.