Custom Fields
Re-Envisioned
Support MB Frontend Submission Prepopulate relationship field with current post id

  • This topic has 18 replies, 2 voices, and was last updated 1 month ago by dp.
  • Creator
    Topic
  • #30107
    Resolved dp
    Participant

    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

Viewing 10 replies - 1 through 10 (of 18 total)
  • Author
    Replies
  • #30142
    Long Nguyen
    Moderator

    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?

    #30144
    dp
    Participant

    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

    #30166
    Long Nguyen
    Moderator

    Hi,

    So I think this case is related to the topic https://metabox.io/support/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.

    #30176
    dp
    Participant

    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.

    #30191
    Long Nguyen
    Moderator

    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://metabox.io/support/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/

    #30195
    dp
    Participant

    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.

    #30235
    Long Nguyen
    Moderator

    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 );
    
    #30264
    dp
    Participant

    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 );

    #30273
    Long Nguyen
    Moderator

    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

    #30274
    dp
    Participant
    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 );
    
Viewing 10 replies - 1 through 10 (of 18 total)
  • You must be logged in to reply to this topic.