Redirect to Parent Post of Child after edit.

Support MB Frontend Submission Redirect to Parent Post of Child after edit.Resolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36120
    Warren JohnstonWarren Johnston
    Participant

    I have a parent post (project) that has many child posts (Project entries)

    They have a relation ship called 'project-entries_to_projects'

    I have an edit button that allows users to edit project entries. (/add-project-entry/?rwmb_frontend_field_post_id={{ post.ID }})

    I am using rwmb_frontend_after_process to manage redirections when forms are submitted.

    When a user edits a project entry I would like to redirect them to the parent project page that entry connects to.

    Is there a way to get the ID or URL of the parent page while inside the hook rwmb_frontend_after_process?

    Thanks so much everyone!

    #36128
    Long NguyenLong Nguyen
    Moderator

    Hi Warren,

    You can try to get the connected post (parent project) based on the post ID (project entry) in the callback function. And redirect to the connected post by ID.

    Something like:

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        if ( 'my-meta-box' === $config['id'] ) {
            $pages = MB_Relationships_API::get_connected( [
                'id'   => 'project-entries_to_projects',
                'from' => $post_id,
            ] );
    
            $p = reset( $pages );
            wp_safe_redirect( get_the_permalink( $p->ID ) );
    
            die;
        }
    }, 10, 2 );

    Read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#getting-connected-items
    https://docs.metabox.io/extensions/mb-frontend-submission/#form-hooks

    #36134
    Warren JohnstonWarren Johnston
    Participant

    Thank you Long, you are a star as always mate - I will give this a try!

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