Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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()){
        my_query->the_post();
        add_custom_field_automatically_new($post->ID,$post->post_author);
    }
}


//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

add_action('save_post','add_custom_field_automatically');
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
add comment

Your Answer

 
discard

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.