Support Forum
Support › MB Frontend Submission › Connecting Child Posts to a Parent Post Automatically.Resolved
Hi Guys
I have a Post Type Called PROJECT (This is the parent port in the relationship)
When I browse to a post of type project I Would like to have an ADD NEW ENTRY button. When I click on that I will go to a form that creates a post of type PROJECT-ENTRY and passes the form the ID of the post this new entry is supposed to connect to.
I have both Post Types already created and I also have the relationship created and can create Project Entries that connect to a parent POST fine in the wp admin back end.
But how do I do the same in the front end in a secure way so users cannot change the parent post their new entry will be connected to etc.
Many thanks!
Hi,
For example: you have a post Project A and a frontend submission form on that post page, then you want to create a post Entry B when submitting the frontend form and connected to the post Project A automatically.
If yes, you can use this custom code
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
MB_Relationships_API::add( get_queried_object_id(), $post_id, 'your-relationship-ID', $order_from = 1, $order_to = 1 );
}, 10, 2 );
Refer to this topic https://support.metabox.io/topic/auto-relationship-on-form-submission/#post-31334
Thanks Long
I actually have a slightly different use case.
On the Post Project A - I would like to be able to place a button that when pressed links to the form to create Entry B and either in the query string or perhaps as post data passes the postid of Project A into the form so when its submitted it automatically links Entry B as a child of Project A.
So really its finding a method of generating the button to sit on the Project A post and then finding a way to process the incoming querystring or post data (which ever is the most secure method idealy) in the form so it has the correct relationship parameters in place for when it creates its Entry B.
Does that make sense?
Hi,
You can create a button with a link to the page that has the frontend form, and pass the Project A post ID to the URL via a custom parameter. Like this
http://your-site.com/frontend-submission-page/?current_post_id=get_queried_object_id()
Then retrieve the project ID via the variable $_GET
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
$project_id = $_GET['current_post_id'];
if( !empty( $project_id ) ) MB_Relationships_API::add( $project_id, $post_id, 'your-relationship-ID', $order_from = 1, $order_to = 1 );
}, 10, 2 );
Hope that makes sense.
Hi Ya
Di I put that code into functions.php? Or somewhere else?
Hi,
You can add the custom PHP code to the file functions.php in the theme/child theme folder or in a custom plugin or use Code Snippets plugin https://wordpress.org/plugins/code-snippets/