I am trying to save custom fields on a custom post type. When I save the data it shows the word "Array" as if I am trying to echo an array. I did a var_dump and it gives me this:

array(1) {


string(5) "Hello"


So I tried echoing it as $_POST['catagory'][0] instead and it gives me the same result.

Here is my code:

 * create meta box and fields for catagories

function littlereds_menu_catagoriesMetaBox()
    add_meta_box('littlereds_menu_catagoriesMeta', "Catagories", "littlereds_menu_catagoriesCallback", "littlereds_cat", "normal");
add_action('add_meta_boxes', 'littlereds_menu_catagoriesMetaBox');

function littlereds_menu_catagoriesCallback($post)
    wp_nonce_field(basename(__FILE__), 'littlereds_catagoryNonce');
    $littlereds_storedMeta = get_post_meta($post->ID);

    <input name="catagory" id="catagory" type="text" value="<?php
            echo esc_attr($littlereds_storedMeta['catagory']);
    ?>" />
    <pre><?php var_dump($littlereds_storedMeta['catagory']); ?></pre>



 * Save Catagories
function littlereds_save_catagoriesMetaData($post_id)
    //Check the save status
    $is_autosave    = wp_is_post_autosave($post_id);
    $is_revision    = wp_is_post_revision($post_id);
    $is_valid_nonce = (isset($_POST['littlereds_catagoryNonce']) &&     wp_verify_nonce($_POST['littlereds_catagoryNonce'], basename(__FILE__))) ? 'true' : 'false';

//Exits script based on save status
if($is_autosave || $is_revision || !$is_valid_nonce)

//Save or update field data
    update_post_meta ($post_id, 'catagory', sanitize_text_field($_POST['catagory']));

add_action('save_post', 'littlereds_save_catagoriesMetaData');

P.S: Is there an easier way to post code on here other than putting 4 spaces in front of every line? sense the Tab key does not work for spacing in a web form it is really annoying.


So yes, the problem is not with the saving, it's how you load the saved data back... See the documentation for get_post_meta: if you dont' provide a meta data key, the function returns all meta data for that post - which has to be returned as an array. Try this instead:

$littlereds_storedMeta = get_post_meta($post->ID, 'catagory', true);

Note the last parameter "true" which will force the function to return a single value instead of, again, an array.

Hope this helps!

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.