Prepopulate relationship field with current post id
Support › MB Frontend Submission › Prepopulate relationship field with current post idResolved
- This topic has 20 replies, 3 voices, and was last updated 3 years, 2 months ago by
esmrosc.
-
AuthorPosts
-
August 10, 2021 at 10:37 PM #30107
dp
Participanton 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
August 11, 2021 at 1:07 PM #30142Long Nguyen
ModeratorHi,
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?August 11, 2021 at 1:32 PM #30144dp
Participantyes, 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
August 12, 2021 at 6:17 AM #30166Long Nguyen
ModeratorHi,
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.
August 12, 2021 at 9:34 AM #30176dp
ParticipantThe 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.
August 12, 2021 at 4:45 PM #30191Long Nguyen
ModeratorHi,
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/August 12, 2021 at 8:13 PM #30195dp
Participanthi 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.
August 13, 2021 at 9:57 PM #30235Long Nguyen
ModeratorHi,
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-actionsFor 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 );
August 15, 2021 at 9:39 AM #30264dp
Participanttrying to get the current post id with
get_queried_object_id()
but nothing gets registered into the db. Also triedget_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 );
August 15, 2021 at 10:56 PM #30273Long Nguyen
ModeratorHi,
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
August 16, 2021 at 12:13 AM #30274dp
Participantdo_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 workdo_action( 'mb_relationships_add', $post_id, $to, $id, $order_from, $order_to );
August 16, 2021 at 12:52 PM #30279Long Nguyen
ModeratorHi,
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 columnfrom
display the post ID created on your screenshot https://pasteimg.com/image/screenshot-2021-08-15-103629-am.c3PInThe function
get_queried_object_id()
retrieves the ID of the current post when viewing, the functionget_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/August 16, 2021 at 7:11 PM #30286dp
Participantso 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
August 16, 2021 at 11:13 PM #30293Long Nguyen
ModeratorHi,
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 );
August 16, 2021 at 11:18 PM #30295dp
Participantyes 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
-
AuthorPosts
- You must be logged in to reply to this topic.