WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to add custom fields automatically to all my custom post types weblogs based on data from the author profile field.

My code:

function add_custom_field_automatically($post_ID) {
    global $wpdb;
    $curauth = get_userdata($author->ID);
    if(!wp_is_post_revision($post_ID)) {
        $themevalue = get_the_author_meta('themeperauthor', $author->ID);
        $themename = 'themeperauthor';
        add_post_meta($post_ID, $themename, $themevalue);

Please help.

share|improve this question

create a simple query and loop over all posts with your function:

//get all your post of that type
my_query = new WP_Query();
my_query->query(array('post_type' => '','posts_per_page' => -1));
if (my_query->have_posts()){
//loop over all post and update meta field with your function
    while (my_query->have_posts()){

//save as your function but with bit of tweaking

function add_custom_field_automatically_new($post_ID,$post_a) {
    if(!wp_is_post_revision($post_ID)) {
        $themevalue = get_the_author_meta('themeperauthor', $post_a);
        $themename = 'themeperauthor';
        add_post_meta($post_ID, $themename, $themevalue);

after you save this remove it (it only needs to run once).

the use your old function from above and hook it to save_post hook so every time a post is saved it will run

share|improve this answer
Thanks Bainternet The problem arise when user changes their profile meta field. It will be great if it can delete and than update by itself – puanthanh Mar 2 '11 at 11:34
How can a user change his profile meta? – Bainternet Mar 2 '11 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.