I'm trying to make this code work . i couldnt figure out why is not working I'm trying to insert a custom field name 'views' to each (wordpress) post a random value ! add_action('publish_page', 'add_custom_field_automatically'); add_action('publish_post', 'add_custom_field_automatically'); function add_custom_field_automatically($post_ID) { global $wpdb; $rand_num = rand(144, 544); if(!wp_is_post_revision($post_ID)) { add_post_meta($post_ID, 'views', '$rand_num', true); } } PHP:
Hey, You wrapped the $rand_num variable in single quotes... You must wrap it in double quotes, in fact, quotes aren't even needed there to pass the value. In other words... It should be: add_post_meta($post_ID, 'views', $rand_num, true); PHP:
Thanks its working... Is there any way i can change/update (a random num) the value for the custom field name 'views' for old post which is already published and has a custom field value already assign! The above code works only for new post in wordpress!
Yea, just run a custom SQL command. Don't use this, but just for the idea: $rand_num = rand(144, 544); mysql_query("UPDATE `{$posts_table}` SET `views`='{$rand_num}' WHERE `custom_field`>'';); PHP:
i try the code below , but no luck , can u help me out $rand_num = rand(144, 544); mysql_query("update wp_postmeta set meta_value = '[B]{$rand_num}[/B]' WHERE meta_key = 'views';" ) PHP: