Wordpress update_post_meta does not update dynamic values

Stuart picture Stuart · Jul 11, 2011 · Viewed 7.2k times · Source

This is a little strange, I am using "update_post_meta" to update the custom fields in Wordpress. When I run update_post_meta($post_id, 'Test_Field', 'Test Value'); the custom field updates without a problem BUT when I use a string value it does not update:

$test_value = "Test";
 echo $test_value; // No problem here
 echo $post_id; // No problem here
 update_post_meta($post_id, 'Test_Field', $test_value);

Thanks very much in advance for your help

Stu

Answer

Vitor de Carvalho picture Vitor de Carvalho · Sep 5, 2012

Try to cast variables with types. Try this:

update_post_meta( (int) $post_id, 'Test_Field', (string) $test_value );