Get featured image thumbnail and inserting into custom field on wordpress posts

You will want to make the following changes to your add_custom_field_automatically function.

First, we will need to add the global $post; as we will need the $post variable inside the function scope. You do this with a comma. more on variable scope here.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb, $post;
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', 'custom value', true);
    }
}

Now instead of returning the thumbnail, we will collect it in a variable.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb, $post;
    $myImage=""; // create a nice empty variable
    if(has_post_thumbnail($post->ID)) {
        $myImage = get_the_post_thumbnail_url($post->ID);
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', 'custom value', true);
    }
}

At this stage, it still does not work because we need to do something with our new variable.

Let’s pass it to the add_post_meta

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb, $post;
    $myImage=""; // create a nice empty variable
    if(has_post_thumbnail($post->ID)) {
        $myImage = get_the_post_thumbnail_url($post->ID);
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', $myImage, true);
    }
}

This will probably work but let’s do some cleanup. We don’t need the $post global because we have the ID already inside $post_ID. So let’s fix the if to use that. We will also give our custom field a nice name.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    $myImage=""; // create a nice empty variable
    if(has_post_thumbnail($post_ID)) {
        $myImage = get_the_post_thumbnail_url($post_ID);
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'the-image-I-need', $myImage, true);
    }
}

And, with that, we have merged the two examples. However, we have also created the possibility of unexpected errors so let’s head those off at the pass.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    $myImage=""; // Create a nice empty variable
    if(has_post_thumbnail($post_ID)) {
        $myImage = get_the_post_thumbnail_url($post_ID);
    }else{
        return; // If there is no thumbnail, stop early.
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'the-image-I-need', $myImage, true);
    }
}

Hopefully, this little stroll through the process will help you understand what we did and why we did it so you can make modifications for your own use case.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)