Support

Account

Home Forums Reply To:

  • Hi.. I have created two CPTs: Events and Sheets. Both CPTs only have one ACF field: a bi-directional Relationship field… So when I – in the backend admin – assign an event to a sheet, this assignment is also directly visible on the Edit Event page (of course).

    For every event there can be an arbitrary number of Sheets, and when I go to an event page on the frontend, I have created a form where I can create new Sheets directly from the frontend. The sheet creation works great, the sheet is created and added to the list of sheets in the admin Sheets section.

    But when I submit my form, I want to send the current Eventpage ID to the sheet_relationship, so that the new sheet automatically will be assigned to the page I am generating it from.

    I guess I am shooting past the target with the following $metaValue, but can you please advice on how I should do this?

    THIS IS THE FORM IN HTML:

    
    <form method="post">
        <p><label for="sheetTitle"><?php _e('Sheet Title:', 'txtdomain') ?></label>
            <input type="text" name="sheetTitle" id="sheetTitle" /></p>
        <?php
        $metaValue = 'a:1:{i:0;s:2:"' . get_the_ID() .'";}';
        ?>
        <input type="hidden" name="event_relationship" value="<?= $metaValue ?>">
        <button name="submit" type="submit"><?php _e('Submit', 'txtdomain') ?></button>
    </form>
    

    THIS IS THE CODE THAT HANDLES THE FORM:

    if (isset( $_POST['sheetTitle']) && isset( $_POST['submit'])) {
        $sheetPostArgs = array(
            'post_title'    => $_POST['sheetTitle'],
            'post_status'   => 'publish',
            'post_type' => 'sheet'
        );
        $sheetID = wp_insert_post( $sheetPostArgs);
        add_post_meta($sheetID, "event_relationship", @$_POST["event_relationship"], true);
    }