Hi Malaga,
Thank you for sharing these details and it seems that our chat got disconnected earlier.
To include the title of grandparent post, when adding the parent, through Toolset Form, you can create a custom shortcode that uses functions "toolset_get_related_posts" ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts ) and "get_posts" ( ref: https://codex.wordpress.org/Template_Tags/get_posts ).
Consider the example of a case where grandparent post type is "region" and parent is "province", and the form is adding a post type "city".
You'll first register a shortcode that gets all the regions and then their child provinces and returns them in a list format that is usable with a generic field.
( ref: https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field )
The following code can be added into the active theme's "functions.php" file:
// generate options for the parent field with grandparent info
add_shortcode('get_parent_field', 'get_parent_field_fn');
function get_parent_field_fn() {
// get all the grandparent
$args = array(
'post_type' => 'region',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
// get children of a grandparent
$query_by_element = $post->ID; // ID of post to get relationship from
$relationship = 'region-province'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 1000; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.
$get_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
for ($i=0; $i < sizeof($get_results); $i++) {
$data[] = array('value' => $get_results[$i], 'label' => get_the_title($get_results[$i]).' ( '.$post->post_title.' )' );
}
}
return(json_encode($data));
}
Next, in your form, you can use this shortcode with a generic field like this:
[cred_generic_field type='select' field='@province-city.parent']
{
"required":1,
"default":[],
"options":[get_parent_field]
}
[/cred_generic_field]
Important Note: You'll replace the post type and post-relationship slugs, with the actual ones used on your website.
As a result, you'll have a list of parent posts with the grandparent's post title automatically appended at the end.
( example screenshot: hidden link )
Please also note that in this field will only show those parent posts, which have a grandparent post attached.
I hope this helps and please let me know how it goes.
regards,
Waqar