I'm trying to add a custom filed name with a value(random value) for all the published post in wordpress ... but got stuck .. Will be grateful to you guys if you can figure out this .. the below code(when inserted in function.php) works for new post but not for old post which are already published . 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(10, 55); if(!wp_is_post_revision($post_ID)) { add_post_meta($post_ID, 'customfieldname', $rand_num, true); } } PHP: I'm also trying to do the same in phpmyadmin , here it works but i 'm not able to generate a random number ... the number generated by rand() are in decimals. i want to genearte numbers between 20 to 100. How this can be done ... INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT wp_posts.ID, 'views', rand() FROM wp_posts WHERE wp_posts.post_status = 'publish' PHP: Hoping for some PHP expert to solve this ... Thanks in advance