Skip Navigation

[Resolved] Parent can't be identified without Grandparent

This support ticket is created 5 years, 9 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.

Our next available supporter will start replying to tickets in about 2.78 hours from now. Thank you for your understanding.

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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Waqar 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1200382

I am trying to: create a form where user can select Parent.

Link to a page where the issue can be seen:

I expected to see: list of parents with indication of the grandparent they belong to.

Instead, I got:the list of parents shows just the parent post title. this isn't specific enough to be able to identify the correct parent post. I need to also be able to see the grandparent post.

#1200388

PS, grandparent>parent is one to many, parent>child is one to many. so each parent belongs to one grandparent.

the only workaround i have is to name the parent post title to include the grandparent title. would prefer to have it automatically displayed somehow.

#1200437

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