1

So, I have a few form fields like so:

<input name='mainStageSatOrder[theband][theid]' type='hidden' class='band-id' value='' />";
<input name='mainStageSatOrder[theband][theorder]' type='hidden' class='band-order' value='' />";

As you can see, these form fields (of which there are more, these are just examples of both types) create a multi-dimensional array like so (I hope, please correct me if I'm wrong):

Array (
[mainStageSatOrder] => Array
    (
        [theband] => Array
            (
                [theid] => 1
                [theorder] => 5
            )

        [theband] => Array
            (
                [theid] => 2
                [theorder] => 8
            )
    )
)

I want these values to use the update_post_meta function to update the relevant fields when the page update is submitted. I know I can hook into the submit action post_submitbox_start action which I understand just fine.

What I'm not sure on, is what the PHP might be once the submit button is clicked. What I want to happen is that when the submit button is clicked, the multidimensional array is looped through using a foreach loop and for each 'theband' sub-array, the two values are used in the update_post_meta function.

foreach(???) {
    update_post_meta( 1, 'theorder', '5' ); //where 1 and 5 are values passed from the MD array
}

So, the process goes:

1) User clicks publish/update button 2) All values from all fields are passed into the multidimensional array 3) The MD array is looped through and, using update_post_meta, the relavent data is updated 4) Confirm yes/no

Thanks.

1

You got a litte thinking problem with your MD array, it should look like that, otherwise you will overwrite data within your form:

["mainStageSatOrder"]=> array(2) {
    [0]=> array(1) {
        ["theband"]=> array(2) {
            ["theid"]=> int(1)
            ["theorder"]=> int(5)
        }
    }
    [1]=> array(1) {
        ["theband"]=> array(2) {
            ["theid"]=> int(2)
            ["theorder"]=> int(8)
        }
    }
}

Now you got two options (I'd recommend option B, because you might not need the single meta data without the context)

Option A: Save your data in single meta fields. You will need the parent_key ($key or 'mainStageSatOrder'), $row_id and field_id ($sub_key or $key) to get your data.

function save($post_id) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( !current_user_can( 'edit_post', $post_id ) || !array_key_exists('mainStageSatOrder', $_POST) )
        return;
    foreach ($_POST['mainStageSatOrder'] as $row_id => $rows) {
        foreach ($rows as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $sub_key => $sub_value) {
                    $meta_key = '_'.$key.'_'.$row_id.'_'.$sub_key;
                    update_post_meta($post_id, $meta_key, $sub_value);
                }
            } else {
                $meta_key = "_mainStageSatOrder_".$row_id."_".$key;
                update_post_meta($post_id, $meta_key, $value);
            }
        }
    }
    update_post_meta($post_id, '_mainStageSatOrder', array_keys($_POST['mainStageSatOrder']));
}

Option B: Save your data in a serialized array (WordPress handles the serialization). To access and do stuff with the array again, just unserialize() the meta_value.

function save_array($post_id) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( !current_user_can( 'edit_post', $post_id ) || !array_key_exists('mainStageSatOrder', $_POST) )
        return;
    update_post_meta($post_id, '_mainStageSatOrder', $_POST['mainStageSatOrder']);
}

Somewhere in your PHP code:

$mainStageSatOrder = unserialize(
    get_post_meta(
        get_the_ID(), 
        '_mainStageSatOrder', 
        true
    )
);
  • OK, thanks :) I'll give both a go and get back to you. – Kev Mar 14 '16 at 15:20
1

First off, there's a problem with your array. Arrays can not have duplicate keys. So, only the first key will be saved. You need to change your form to something like this.

<input name='mainStageSatOrder[theband0][theid]' type='hidden' class='band-id' value='' />";
<input name='mainStageSatOrder[theband0][theorder]' type='hidden' class='band-order' value='' />";
<input name='mainStageSatOrder[theband1][theid]' type='hidden' class='band-id' value='' />";
<input name='mainStageSatOrder[theband1][theorder]' type='hidden' class='band-order' value='' />";

The array will look like this

$array = array(
    'mainStageSatOrder' => array(
        'theband0' => array(
            'theid' => 1,
            'theorder' => 5
        ),
        'theband1' => array(
            'theid' => 2,
            'theorder' => 8
        )
    )
);

You don't need a foreach loop while saving the meta data. You can instead save it as an array. WordPress will automatically serialize it for you.

$array = $_POST['mainStageSatOrder'];
update_post_meta( $postid, 'mainStageSatOrder', $array );  

And, while retriving the values..

$data = get_post_meta($post->ID, 'mainStageSatOrder', true);  

The returned $data will be an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.