Support Forum
Support › MB Frontend Submission › Prepopulate relationship field with current post idResolved
Hi,
The function get_queried_object_id()
get the current page ID that show the frontend form as well.
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
MB_Relationships_API::add( get_queried_object_id(), $post_id, 'events_to_posts', $order_from = 1, $order_to = 1 );
}, 10, 2 );
Do you add the frontend shortcode to the events
page directly? Or add through a builder template?
Or just add the fixed ID of the events
page like this
MB_Relationships_API::add( 1234, $post_id, 'events_to_posts', $order_from = 1, $order_to = 1 );
The function get_queried_object_id() get the current page ID that show the frontend form as well.
- yes tried it but returns 0. (pls see my reply #30264)
Do you add the frontend shortcode to the events page directly? Or add through a builder template?
- tested frontend shortcode on page directly and in builder template, no difference
Or just add the fixed ID of the events page like this
MB_Relationships_API::add( 1234, $post_id, 'events_to_posts', $order_from = 1, $order_to = 1 );
- yes, i understand this will definitely work but this is what im trying to get -> pls see my reply #30176 ps- frontend form will be on every CPT page with different ID
thank you
Hi,
It works well on my local site, please take a look at this demo https://www.loom.com/share/3d05e4f30e0a4c1cb03b92d179792b2c
thank you for ur patience. it works. i had the ajax on. thank you again Long
Hi, This was exactly what I was also looking for and I got it working. Thank you. However I would like to add an extra part to it, so that if you wished to edit the details of the post on the front end, on the generated post, it would not then attach its own Post ID to the relationship field again.
ie, I have 'clients' and 'equipment' as my related custom post types. when on a 'client' cpt i have the front end form to register new equipment that is related and linked to them. That is using the post id relationship code that you have provided here. But I also wish to be able to edit the equipment details using the front end form, and to do this I had the edit form on the 'equipment' post type.
What I then realised is that the post id of the equipment that was being edited was then also being added to the same relationship field. I must only have 1 post id in this field as I use the value to link 'gravity form' submissions back to the 'client' that relate to the 'equipment'.
I was imagining that perhaps it might be possible to make the function conditional on it taking place on the 'client' post type, so that when you are editing the 'equipment' no post id is set to the relationship? I hope that is a clear enough description.
Thanks in advance to any help or guidance you can provide.
<div>
//Add a connection to the post itself after submitting the post
add_action( 'rwmb_frontend_after_process',
function( $config, $post_id )
{
MB_Relationships_API::add( get_queried_object_id(), $post_id, 'clients-and-equipment', $order_from = 1, $order_to = 1 );
}, 10, 2 );
</div>
So, I came back to this today and with a bit more digging around I got it to work, just had to add this condition to the snippet -
if ( is_singular( 'client' ) )
full snippet below if useful for anyone else. I know its super simple like i thought it should be, but I am very happy with it working!
//Add a connection to the post itself after submitting the post
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
if ( is_singular( 'client' ) ){
MB_Relationships_API::add( get_queried_object_id(), $post_id, 'clients-and-equipment', $order_from = 1, $order_to = 1 );}
}, 10, 2 );