Support Forum
Support › MB Frontend Submission › Prepopulate relationship field with current post idResolved
on my frontend form, how do i prepopulate the 'relationship' field with the current post id, so there's no need for users to select the related post (which is the current post the form is on) eg.eg
Hi,
The frontend form helps you to create new posts on the front end. The post is not created so where we can get the relationship
to repopulate? Does that mean you want to set the relationship on the backend and don't allow the user to set the relationship on the frontend?
yes, the user will not need to set the relationship as the post they are on (when they fill the form), should be the related post
Hi,
So I think this case is related to the topic https://support.metabox.io/topic/only-showing-certain-custom-fields-on-the-frontend-submission/
You can remove the meta box on the frontend and show it on the backend only.
The backend will not need to set the relationship as well because the 'current post' (which the form is on), should automatically be the 'related' post to their 'submission'.
eg. frontend form is on ./post-69. User fill up and submit this form. This submitted entry automatically is related to ./post-69.
Hi,
Thanks for the additional information.
So you can use the action rwmb_{$field_id}_after_save_field
to update the field value after submitting/updating a post. Refer to this topic
https://support.metabox.io/topic/using-custom-meta-fields-to-create-cpt-title/
And documentations
https://docs.metabox.io/actions/#rwmb_after_save_field
https://docs.metabox.io/rwmb-set-meta/
hi long,
im confused. 1) where can i find the field_id. i created using the relationship builder.
2) thank you for ref 🙂 but will the set-meta work for the related post also, since post-meta and the relationship values are saved in different db.
Hi,
Sorry, it looks like the field post. You can use the public API of Relationships to add the relationship to a post itself after submitting the post. Please read more on the documentation
https://docs.metabox.io/extensions/mb-relationships/#creating-connections-programmatically
https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions
For example:
//Register relationship
add_action( 'mb_relationships_init', function() {
MB_Relationships_API::register( [
'id' => 'posts_to_posts',
'from' => 'post',
'to' => 'post',
'reciprocal' => true,
] );
} );
//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( $post_id, $post_id, 'posts_to_posts', $order_from = 1, $order_to = 1 );
}, 10, 2 );
trying to get the current post id with get_queried_object_id()
but nothing gets registered into the db. Also tried get_the_ID()
.
https://pasteimg.com/image/screenshot-2021-08-15-103629-am.c3PIn
here's the code
MB_Relationships_API::add( $post_id, get_queried_object_id(), 'events_to_posts', $order_from = 1, $order_to = 1 );
Hi,
If you use the frontend hook like the example code, please use the variable $post_id
to add the connection to the submitted post.
Add a connection to the post itself after submitting the post
do_action( 'mb_relationships_add', $from, $to, $id, $order_from, $order_to );
so from the documentation -> the submitted post is $post_id, but my question is how do i get the current post id $to
when get_the_ID() and get_queried_object_id() doesnt work
do_action( 'mb_relationships_add', $post_id, $to, $id, $order_from, $order_to );
Hi,
The problem is you want to execute the function add()
after submitting the frontend form. So we can only use the variable $post_id
to point to the post that is created. You can also see that the column from
display the post ID created on your screenshot https://pasteimg.com/image/screenshot-2021-08-15-103629-am.c3PIn
The function get_queried_object_id()
retrieves the ID of the current post when viewing, the function get_the_ID()
retrieves the ID of the post in the loop so we should not use them in this hook.
https://developer.wordpress.org/reference/functions/get_the_id/
https://developer.wordpress.org/reference/functions/get_queried_object_id/
so close, hate to give up 🙁
so any idea how to get the current post id for me to insert into the $to
do_action( 'mb_relationships_add', $post_id, $to, $id, $order_from, $order_to );
thanks Long
Hi,
I've tried many times to explain that the variable $post_id
is pointing to the submitted post ID. So you can just replace $to
with $post_id
Please check my code again
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
MB_Relationships_API::add( $post_id, $post_id, 'posts_to_posts', $order_from = 1, $order_to = 1 );
}, 10, 2 );
yes but by doing that, im creating a relationship from the submited post_id to the submited post_id; which isnt what im looking for and redundant