user Profile meta value as custom field

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:

Read More
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.

Related posts

Leave a Reply

1 comment

  1. 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');