Skip Navigation

[Resolved] Parent post is moved to child post after form submission

This support ticket is created 4 years, 3 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)

Tagged: 

This topic contains 12 replies, has 2 voices.

Last updated by Shane 4 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1459845

I have a form inside Parent post which is supposed to create child posts.
After i submit the child post creation form the parent post is moved as a the child post.

function form_parent_hidden_field() {
    global $post;
echo sprintf( '<input type="hidden" name="parent_id" value="%d">', $post->ID );
}
add_action( 'af/form/hidden_fields/key=form_5e1eec730ffee', 'form_parent_hidden_field' );

function form_connect_to_parent( $post ) {
    $parent_id = $_POST['parent_id'];
    
    toolset_connect_posts( 'department-image-date-rating', $parent_id, $post->ID );
}
add_action( 'af/form/editing/post_created/key=form_5e1eec730ffee', 'form_connect_to_parent' );

Im using this simple code. First function is getting the current post(parent) the id and the second function is supposed to create a child post and connect it with toolset_connect_posts.

Any help is appreciated.

Many thanks

#1460175

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

If I get your scenario correctly you essentially want the child post to be connected to the parent post automatically since the form is on the parent post itself correct?

Please let me know.

Thanks,
Shane

#1460247

Exactly...truth is with this code it was working properly some months ago. The ACF form on sumbit creates a child post of the parent. But after a while does this weird behavior now. On submit now it transfer the parent post to child post.

I disabled everything and reverted to default theme with just Types, ACF custom fields and Forms. Still does this weird behaviour.
Any idea why the code stopped working?

#1460303

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

What you can do is to see what is being passed in the parent id variable.
Try doing this

function form_connect_to_parent( $post ) {
    $parent_id = $_POST['parent_id'];
     die('parent ID'.$parent_id);
    toolset_connect_posts( 'department-image-date-rating', $parent_id, $post->ID );
}
add_action( 'af/form/editing/post_created/key=form_5e1eec730ffee', 'form_connect_to_parent' );


So you can check to see what is being passed in the parent id.

Thanks,
Shane

#1461449

Hello Shane

Nothing is passed to the parent. After submit the department post for example T2 moves and becomes child [image-date-rating]/T2.

Do you want to check it live?

Thank you
Enea

#1462175

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

The problem is that your toolset_connect_posts() function is using the $parent_id variable. What I need to know is if the correct ID is being passed.

Given that you are using the $_POST variable does that mean the parent id is somewhere on the ACF form ?

Please let me know.
Thanks,
Shane

#1462185

I suppose It is added from the first fucntion which selects the current post[parent].

function form_parent_hidden_field() {
    global $post;
echo sprintf( '<input type="hidden" name="parent_id" value="%d">', $post->ID );
}
add_action( 'af/form/hidden_fields/key=form_5e1eec730ffee', 'form_parent_hidden_field' );
 

Do you want to take a look online?

Many thanks
Enea

#1462255

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

Perhaps it would be best to provide me with admin access to the website so that I can check to see what exactly is happening.

I've enabled the private fields for your next response.

Also please let me know where I can find the form on the frontend and test out the issue. I also want to know where the code is located.

Thanks,
Shane

#1464039

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

Is there a reason for not using our Toolset forms to do this ?

With our Toolset forms you can automatically set the parent relationship for the current page that this form is on.

Please let me know.
Thanks,
Shane

#1464043

Hello Shane.

We are using this form because integrates well with ACF fields. I dont think Toolset Forms can do that.

Did you have a chance to test the code?

Thank you

#1464459

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

I tried editing the code and refresh the page but it seems you might have taken the form off the page.
hidden link

I'm not seeing the form anymore.

Thanks,
Shane

#1468759

Good morning,
Thats what im saying the issue is.
When you submit the form from the parent custom post hidden link
it become child hidden link

Changes from parent to child
Any idea why is that?

#1469397

Shane
Supporter

Languages: English (English )

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

Hi Stuart,

I'm getting an error 500 on the link you sent above for
hidden link

However I'm looking at your code.

function form_parent_hidden_field() {
    global $post;
echo sprintf( '<input type="hidden" name="parent_id" value="%d">', $post->ID );
}
add_action( 'af/form/hidden_fields/key=form_5e1eec730ffee', 'form_parent_hidden_field' );
 
function form_connect_to_parent( $post ) {
    $parent_id = $_POST['parent_id'];
     
    toolset_connect_posts( 'department-image-date-rating', $parent_id, $post->ID );
}
add_action( 'af/form/editing/post_created/key=form_5e1eec730ffee', 'form_connect_to_parent' );

From what I can see the Parent post ID and the current post ID are the same. You are setting the parent post ID with this line here

   global $post;
echo sprintf( '<input type="hidden" name="parent_id" value="%d">', $post->ID );

However when this form is submitted you are submitting it with the parent ID. Then in the other function here

    $parent_id = $_POST['parent_id'];
     
    toolset_connect_posts( 'department-image-date-rating', $parent_id, $post->ID );

You are setting the $post->ID to the same as the parent ID. I'm assuming you want the ID of the current post that the form is creating correct?

If so then you will need to check with the ACF team in order to know the current ID of the post that the form is creating. Essentially the best approach to this would be to use the.

You will probably need to use something like this below.
hidden link

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.