Prepopulate relationship field with current post id

Support MB Frontend Submission Prepopulate relationship field with current post idResolved

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #30107
    dpdp
    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

    #30142
    Long NguyenLong 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
    dpdp
    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 NguyenLong Nguyen
    Moderator

    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.

    #30176
    dpdp
    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 NguyenLong 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://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/

    #30195
    dpdp
    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 NguyenLong 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
    dpdp
    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 NguyenLong 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
    dpdp
    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 );
    
    #30279
    Long NguyenLong Nguyen
    Moderator

    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/

    #30286
    dpdp
    Participant

    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

    #30293
    Long NguyenLong Nguyen
    Moderator

    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 );
    #30295
    dpdp
    Participant

    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

Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.