1

I've just noticed that all of my values in this array, which are either integer or null, when I use update_post_meta, it stores both the integer and null value as a string, rather than an object.

When I var_dump on the output from get_post_meta, I get this:

array (size=1)
  0 => 
    array (size=5)
      0 => string '238' (length=3)
      1 => string '234' (length=3)
      2 => string 'null' (length=4)
      3 => string 'null' (length=4)
      4 => string 'null' (length=4)

Why is this, and how can I fix this?

function bv_faq_procedure_meta_box_save($post_id) {
    // Check the nonce
    if(!isset($_POST['bv_faq_procedure_meta_box_nonce']) || !wp_verify_nonce($_POST['bv_faq_procedure_meta_box_nonce'], basename( __FILE__ ))) {
    return;
    }

    // Check the user's permissions.
    if(!current_user_can('edit_post', $post_id)) {
    return;
    }

    $bv_faq_procedures = array(
        isset($_POST['bv_faq_procedure_meta_1']) ? $_POST['bv_faq_procedure_meta_1'] : null,
        isset($_POST['bv_faq_procedure_meta_2']) ? $_POST['bv_faq_procedure_meta_2'] : null,
        isset($_POST['bv_faq_procedure_meta_3']) ? $_POST['bv_faq_procedure_meta_3'] : null,
        isset($_POST['bv_faq_procedure_meta_4']) ? $_POST['bv_faq_procedure_meta_4'] : null,
        isset($_POST['bv_faq_procedure_meta_5']) ? $_POST['bv_faq_procedure_meta_5'] : null
    );

    // Save meta
    if(isset($_POST['bv_faq_procedure_meta_1']) || isset($_POST['bv_faq_procedure_meta_2']) || isset($_POST['bv_faq_procedure_meta_3']) || isset($_POST['bv_faq_procedure_meta_4']) || isset($_POST['bv_faq_procedure_meta_5'])) {
        update_post_meta($post_id, 'Procedure', $bv_faq_procedures);
    } else {
    // delete data
        delete_post_meta($post_id, 'Procedure');
    }
}
add_action('save_post_faq', 'bv_faq_procedure_meta_box_save', 10, 2 );
Lee
  • 4,187
  • 6
  • 25
  • 71
  • I think if you `var_dump($bv_faq_procedures)` before saving meta, you'll see your `null` but the conversion to **string** happens in the `update_post_meta` function, if you could show us the code of that function we could confirm or refute this – teeyo Mar 01 '18 at 15:28
  • It's a core Wordpress function - https://codex.wordpress.org/Function_Reference/update_post_meta – Lee Mar 01 '18 at 15:29
  • Well just by reading the page you just add to the comment, you got this interesting line : **$meta_value (mixed) (required) The new value of the custom field. A passed array will be serialized into a string.(this should be raw as opposed to sanitized for database queries)** – teeyo Mar 01 '18 at 15:35
  • OKay, so can you explain that, because I read that too, but don't understand it. – Lee Mar 01 '18 at 15:36
  • I think I understand after a quick Google, I need to unserialise the value when reading from the meta? http://php.net/manual/en/function.unserialize.php – Lee Mar 01 '18 at 15:38
  • I don' t think you have to do that, because I think the wordpress core should do it for you before updating, I'll read the code of that method and try to understand why the `null` becomes `'null'`, I have to admit it, it's strange ! – teeyo Mar 01 '18 at 15:40
  • I've just noticed it does the same with integers too. When I select a value in my dropdown, the value is the page ID, which gets stored as a string, not an integer. All the array values are string. – Lee Mar 01 '18 at 15:41
  • Actually in the docs, it says that the param if not scalar it should be **serialized**, so I think you should serilize the array before passing it to the function. – teeyo Mar 01 '18 at 15:42

1 Answers1

0

Looking at the source code of the function, it tells you hat you need to serialize the array yourself if it's not a scalar type :

@param mixed  $meta_value Metadata value. Must be serializable if non-scalar.

So try this out :

$bv_faq_procedures = array(
    isset($_POST['bv_faq_procedure_meta_1']) ? $_POST['bv_faq_procedure_meta_1'] : null,
    isset($_POST['bv_faq_procedure_meta_2']) ? $_POST['bv_faq_procedure_meta_2'] : null,
    isset($_POST['bv_faq_procedure_meta_3']) ? $_POST['bv_faq_procedure_meta_3'] : null,
    isset($_POST['bv_faq_procedure_meta_4']) ? $_POST['bv_faq_procedure_meta_4'] : null,
    isset($_POST['bv_faq_procedure_meta_5']) ? $_POST['bv_faq_procedure_meta_5'] : null
);

// Here you serialize your array.
$serializedArray = serialize($array);

// Save meta
if(isset($_POST['bv_faq_procedure_meta_1']) || isset($_POST['bv_faq_procedure_meta_2']) || isset($_POST['bv_faq_procedure_meta_3']) || isset($_POST['bv_faq_procedure_meta_4']) || isset($_POST['bv_faq_procedure_meta_5'])) {
    // And you provide the serialized array insead.
    update_post_meta($post_id, 'Procedure', $serializedArray);
} else {
// delete data
    delete_post_meta($post_id, 'Procedure');
}
teeyo
  • 3,665
  • 3
  • 22
  • 37
  • I'll try this, although I have another question... you see in my var_dump, it's a nested array. Why is this, and can this be addressed? So the initial [0] is removed, and the array has the direct keys [0] to [4]? – Lee Mar 01 '18 at 15:51
  • I think once you'll use the serialized array, the output will be different, if not, you have no choice but to use `$output = array_values($output[0])` so you'll get a simple 1 dimension array :) – teeyo Mar 01 '18 at 15:55