I am trying to update the value in a post custom field as automatically with a variable value.

  • Hi there,

    I am trying to update the value in a post custom field (a function to check all posts) after the post author has changed its local avatar/profile picture.

    In every post, there is a special custom field that retrieves the url of the locally stored avatar image of the post author (it´s not gravatar nor default wp icons). Now, users (authors) can replace their avatar which is stored as a user_meta in the field key "avatar_image_id".

    I would like to update the value of the post custom field/post meta "avatar" with the new url of the replaced image. So far I have this code in theme functions.php, but I am not returning any value :/, only blank. Can you help?

    //Update avatar custom field post meta function
    function update_avatar_meta() {
        $args = array(
            'post_type' => 'post', // Only get the posts
            'post_status' => 'publish', 'draft', 'pending', 'future', 'trash', 'auto-draft', 'inherit', // Only the posts that are in these status
            'posts_per_page'   => -1, // Get every post
            'post_author' => 'the_author'
        //Find ID of attachment saved user meta and get URL
        $saved_id = get_the_author_meta( ID, 'avatar_image_id', true );
            //return url saved image
            return wp_get_attachment_url( $saved_id ); 
        $posts = get_posts($args);
        foreach ( $posts as $post ) {
            // Run a loop and update every meta data
            update_post_meta( $post->ID, 'avatar', $saved_id);
    // Hook into init action and run our function


Log in to reply