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;
}
I apologize for sending again, but I need to close issue this asap!
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
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.
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
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.
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.
My issue is resolved now. Thank you!
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]);
You are right, it need to push new seaman ID into array, thanks for sharing the solution.
My issue is resolved now.