Hi,
Thank you for contacting us and I'd be happy to assist.
Based on the solution that Minesh provided in the other support thread, you'll need to update the code, so that:
- it gets the custom user field value for the target manufacturer's ID
- next from that manufacturer's ID, it gets the related Robots posts, using the 'toolset_get_related_posts' function
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )
- and lastly, sets the query for the relationship filter to only those related Robots.
Here is what that code will look like:
function filter_parent_post_select2_by_manufacturer( $query ){
// check for specific action
if ( (!empty($_REQUEST['action'])) && ($_REQUEST['action'] == 'select2_potential_relationship_parents') && ( $_REQUEST['slug'] == 'robot-case' ) ) {
// get user custom field value
$manufacturer_id = types_render_usermeta( 'manufacturer-id', array('user_current' => true) );
// if user custom field value exists
if( !empty($manufacturer_id)) {
// get related robots
$get_related_robots = toolset_get_related_posts( $manufacturer_id, 'manufacturer-robot', 'parent', 99999, 0, array(), 'post_id', 'child' );
// if related robots exist, set query to only include them
if(!empty($get_related_robots)) {
$query->set( 'post__in', $get_related_robots );
}
}
}
}
add_action( 'pre_get_posts', 'filter_parent_post_select2_by_manufacturer', 101, 1 );
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Note: You'll replace:
- robot-case: with the actual relationship slug of Robot Case relationship
- manufacturer-id: with the actual slug of your user custom field
- manufacturer-robot: with the actual slug of your Manufacturer Robot relationship
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar