Skip Navigation

[Resolved] Setting up parent post in front end

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to set the parent post automatically when creating a chid post. Now the user will CREATE that parent post and when creating the child that parent should be set automatically.

Solution:

Add the following to your functions.php file

// Add Shortcode
function user_post_id( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'author_id' => '',
            'cpt' => '',
        ),
        $atts
    );
    $args = array(
        'author'        =>  $atts['author_id'],
        'orderby'       =>  'post_date',
        'order'         =>  'ASC',
        'posts_per_page' => 1,
        'post_type'=> $atts['cpt']
        );
    $post=get_posts($args); 
    return $post[0]->ID;
 
}
add_shortcode( 'user_post_id', 'user_post_id' );

Then on your CRED form where it has the Parent Field on the edit form page add this as the value [user_post_id author_id="[wpv-current-user info='id']" cpt='post']

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 20 replies, has 2 voices.

Last updated by geoffD 6 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#748585

Dear Sir/Madam

I have a relationship set up between two CPT:

User Will (Parent)
Pet (Child)

I am aware that you can create a new child Pet directly from the back end parent User Will page and that you can also select which User Will is the parent from the back end child Pet page.... But...

What I am trying to do is create a new child Pet using a CRED form on the front end. When I create a new Pet it doesn't add the Parent relationship.

Please can you help
best regards

Geoff

#749800

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

Thank you for contacting our support.

If your child cred form doesn't already have this field on then you can add it by editing your form and then click on the Add Post Fields button and then adding the field for the parent.

Please let me know if this helps.
Thanks,
Shane

#750311

Hello Shane

Thank you for your response. I was aware that you could do it like this however this way means the user has to manually select the parent User Will in the form.

What I want to happen is for the child Pet to automatically make the parent-child connection. A User only has one User Will so I would need to pass to the form the ID of the users first and only User Will.

Many thanks

Geoff

#752349

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

This can be done if you add the Child Post link to the parent post.

How this works is that if a user visits the parent post they can click on a link to create the child post and then this child will automatically have that parent post as their parent.

Please let me know if this helps.

Thanks,
Shane

#756544

Hi Shane

Thanks for your reply. My user does not start on the Parent page sadly so when they click on the 'add new child Pet" I need some hidden code in the form to find the Parent User Will of the logged in user and add it to the form to make the connection.

Thanks

Geoff

#770173

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

This would beg the question how would the custom code know which parent post the user will want to have attached to his child post ?

#803126
Screen Shot 2018-05-03 at 13.20.19.png

Hi Shane

The User only has one Parent User Will CPT so the child PET CPT needs to find the parent ID from the current user logged in...

Also I notice that in my version of Toolset I have no Relationship selection in the admin area(I have attached a screenshot from one of your tutorial videos) Is this a feature that has been removed?

Many thanks

Geoff

#804050

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geof,

The relationship section is actually in our Latest Types Beta version.

So the user will only be able to create 1 parent CPT ? All his child posts should then only be attached to that one CPT ?

Please let me know.
Thanks,
Shane

#806408

Great Shane...How do I access the beta version?

Also still need a solution to adding the parent ID to the child via user ID

Thanks

Geoff

#806857

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

The beta can be downloaded from the link below.
https://toolset.com/account/downloads/

Select Beta from the dropdown.

Also my question is that If the user will be the one to create the parent ? Will the user only be allowed to create one parent ? Once we know how to get the Parent based on that current user then we can have it automatically add the parent.

Thanks,
Shane

#807068

Hi Shane

Thanks for the beta link...I will take a look.

Here is the relationship:

User Has One User_Will
User_Will Has Many Pets

The user initially creates the User Will by a CRED form however I'm still trying to find a way to only create one User Will as if the user goes back to that page it creates a new one... Then the user navigates to a new page where they select to create a new Pet which they can do several times. In the end a new page shows the User Will and then the list of Pets connected to the User Will.

Hope that makes sense.

Best wishes

Geoff

#813106

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

Ok so essentially what we want here is to get the parent information that the user created and insert it into the child post automatically.

Can you confirm this then we can proceed.

Thanks,
Shane

#814567
flow-1.jpg

Hi Shane

Yes this is what we want to achieve please.

I have attached a flow diagram to illustrate this. The two things I want to achieve are:

1. A way for the user to only create one User_Will(CPT)
2. A way for the user to create many Pets(CPT) that have the above parent User_Will

Hope this makes sense

Best wishes

Geoff

#816199

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

Thank you for the patience.

Add the following to your functions.php file


// Add Shortcode
function user_post_id( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'author_id' => '',
			'cpt' => '',
		),
		$atts
	);
	$args = array(
	    'author'        =>  $atts['author_id'],
	    'orderby'       =>  'post_date',
	    'order'         =>  'ASC',
	    'posts_per_page' => 1,
	    'post_type'=> $atts['cpt']
	    );
	$post=get_posts($args);	
	return $post[0]->ID;

}
add_shortcode( 'user_post_id', 'user_post_id' );

Then on your CRED form where it has the Parent Field on the edit form page add this as the value [user_post_id author_id="[wpv-current-user info='id']" cpt='post']

Please try this and on the frontend it should auto select the parent on the frontend.

Thanks,
Shane

#825107
Screen Shot 2018-05-05 at 15.56.29.png

Hi Shane

The code looks good but it's still not working... I'm just getting the select box saying -not set- for the parent field. I have attached a screenshot to demonstrate ..

Best wishes

Geoff