Skip Navigation

[Resolved] One to many relationship question

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 9 replies, has 3 voices.

Last updated by Minesh 7 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2644123

I am currently trying to create a one to many relationship form that captures both the current page the user is on (done) and link it to a post that is automatically created when a user registers.

This post is the only post that the user will ever author on the site and is used to link completed modules to her profile. My issue currently is that I would like the CRED relationship form to auto-populate the relationship field with the automatically created post authored by my currently logged in user.

the post created as of now is a custom post type called 'completed-modules' and the automatically generated post is always titled the user_id of the registrant. IE. "15"

so my first thought was to write out a shortcode that wrote the shortcode for the CRED form:

add_shortcode( 'my_user_ID', 'user_id_callback' );
function user_id_callback( $atts, $content = null ) {
// (int) The current user's ID, or 0 if no user is logged in.
return do_shortcode(
'[cred-relationship-form form="completed-module-form" parent_item="$current" child_item="' . get_current_user_id() . '"]'
);
}

So this does do what I intended it to do, except the cred form doesn't actually work, which I'm not totally surprised by but would love some insight into how to get this to work properly.

Thanks,

#2644219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

In my opinion, Its not good idea to have post saved with only User ID as post title as it will have the same post slug.

Are you changing the post slug when you create the post dynamically/automatically?

If you want to keep that way I do not have any issue.

Can you please tell me what is the post slug created when you create the post dynamically?

In addition to that - Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#2645267

Hey Minesh,

The Slug for the dynamically created posts does not really matter at all. These only exist to have a relationship formed between them as the user completes courses on the site.

#2645353

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Minesh is on holiday today which is why I'm following up on this ticket.

Can you update your custom shortcode slightly to see if it makes any difference?


add_shortcode( 'my_user_ID', 'user_id_callback' );
function user_id_callback( $atts, $content = null ) {
	// (int) The current user's ID, or 0 if no user is logged in.
	ob_start();
	echo do_shortcode( '[cred-relationship-form form="completed-module-form" parent_item="$current" child_item="' . get_current_user_id() . '"]' );
	return ob_get_clean();
}

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2645357

Hey Waqar,

So this essentially runs into the same problem, once logged the form loads properly but it doesn't submit and the child element appears empty (the Value is properly set to the user ID on inspection) but when you try to submit it just does nothing.

If you hit submit it just has a Ajax error, If you were to remove the child element from the shortcode and just manually find the post it works.

#2645911

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me what post you want to see when you load the form as well as admin access details and problem URL where I can see the form.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2647571

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean that you want to connect to the child where child post author is current loggedin user as you shared that the logged in user will have one post when user is created.

#2648063

Yes the child in the form should always be the post authored by the user in completed modules that is automatically created when the user registers.

#2648325

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share one frontend user access details for whom you created the child post and tell me what child post you created for that frontend user then I will try to adjust the code for the form accordingly.

I have set the next reply to private which means only you and I have access to it.

#2648717

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've moved the shortcode to "Custom Code" section offered by so its easy to edit and adjusted it as given under:
=> hidden link

add_shortcode( 'my_user_ID', 'user_id_callback' );
function user_id_callback( $atts, $content = null ) {
    // (int) The current user's ID, or 0 if no user is logged in.
global $current_user;

 $args = array(
                'post_type'        => 'completed-module',
                'author__in'        => array($current_user->ID),
                'post_status'      => 'publish');

            $author_posts = get_posts( $args );
            $child_id=0;
            if(!empty($author_posts)) {
                     $child_id = $author_posts[0]->ID;
             }
             
    ob_start();
    echo do_shortcode( '[cred-relationship-form form="completed-module-form" parent_item="$current" child_item="' . $child_id . '"]' );
    return ob_get_clean();
}

Now, when I login using the frontend user you shared and check the following page:
- hidden link

I can see the post with title 16 is automatically gets selected with the post relationship form.

Can you please confirm it works as expected now.

#2651511

Hey Minesh,

Sorry I was away for the past week, this looks perfect. Thanks you so much for your help with this.

barrettf confirmed that the issue was resolved on 2023-10-10 12:23:12.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.