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
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
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?
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
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
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
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
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
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
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
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
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?
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