Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom fields clear entries after 2nd time editing WordPress

  • SOLVED

I have some custom fields set up and it saves the values fine at first. The problem is, if you leave a field blank when you first save draft or publish, you can't go back and enter a value because it will still remains blank.

Here is the code that I currently have in my functions.php:


function meta_release_setup()
{
global $post;

$release['Album Title'] = get_post_meta($post->ID,'Album Title',TRUE);
$release['Artist Name'] = get_post_meta($post->ID,'Artist Name',TRUE);
$release['Label'] = get_post_meta($post->ID,'Label',TRUE);
$release['Year'] = get_post_meta($post->ID,'Year',TRUE);
$release['Artist Website'] = get_post_meta($post->ID,'Artist Website',TRUE);
$release['Tracklist'] = get_post_meta($post->ID,'Tracklist',TRUE);

include(TEMPLATEPATH . '/post-meta-release.php');

// create a custom nonce for submit verification later
echo '<input type="hidden" name="meta_release_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}

function meta_release_save($post_id)
{
// authentication checks

// make sure data came from our meta box
if (!wp_verify_nonce($_POST['meta_release_noncename'],__FILE__)) return $post_id;

// check user permissions
if ($_POST['post_type'] == 'page')
{
if (!current_user_can('edit_page', $post_id)) return $post_id;
}
else
{
if (!current_user_can('edit_post', $post_id)) return $post_id;
}

// authentication passed, save data

// var types
// single: _my_meta[var]
// array: _my_meta[var][]
// grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]

foreach ($_POST['release'] as $field => $value):
$current_data = get_post_meta($post_id, $field, TRUE);
$new_data = $value;

if ($current_data)
{
if (is_null($new_data)) delete_post_meta($post_id,$field);
else update_post_meta($post_id,$field,$new_data);
}
elseif (!is_null($new_data))
{
add_post_meta($post_id,$field,$new_data,TRUE);
}
endforeach;

return $post_id;

}


Thanks!

Answers (2)

2011-07-12

Denzel Chia answers:

Hi,

Try changing your codes to update only and not check if there is previous value.
You function meta release setup should get the previous enter post meta value and fill up the inputs for submission. This way you will not need to check for old value, because it will be posted back if there is no changes.

Please try the below codes.


function meta_release_save($post_id)

{

// authentication checks



// make sure data came from our meta box

if (!wp_verify_nonce($_POST['meta_release_noncename'],__FILE__)) return $post_id;



// check user permissions

if ($_POST['post_type'] == 'page')

{

if (!current_user_can('edit_page', $post_id)) return $post_id;

}

else

{

if (!current_user_can('edit_post', $post_id)) return $post_id;

}



// authentication passed, save data



// var types

// single: _my_meta[var]

// array: _my_meta[var][]

// grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]



foreach ($_POST['release'] as $field => $value):


$new_data = $value;

update_post_meta($post_id,$field,$new_data,TRUE);


endforeach;

}


Thanks.
Denzel


Jeremy Phillips comments:

That didn't seem to make a difference. Thanks though.

2011-07-13

Alex Sancho answers:

if you need to clear the values when this are empty, my suggestion is to change the current code to:


<?php
function meta_release_save($post_id)
{
// authentication checks
// make sure data came from our meta box

if (!wp_verify_nonce($_POST['meta_release_noncename'],__FILE__)) return $post_id;

// check user permissions

if ($_POST['post_type'] == 'page')
{
if (!current_user_can('edit_page', $post_id)) return $post_id;
}
else
{
if (!current_user_can('edit_post', $post_id)) return $post_id;
}

// authentication passed, save data
// var types
// single: _my_meta[var]
// array: _my_meta[var][]
// grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]
foreach ($_POST['release'] as $field => $value)
{
$new_data = $value;

if ($new_data != '')
{
update_post_meta($post_id, $field, esc_attr($new_data));
}
else
{
delete_post_meta($post_id,$field);
}
}

return $post_id;
}


Jeremy Phillips comments:

That did the trick! Thanks so much!