Getting the Featured image URL and inserting it as Custom Field on Post update / publish

I've tried a number of solutions posted here regarding converting custom fields to featured images - of course in reverse; but non seem to work for me.

What I'm trying to do is grab the Featured Image URL once the image is uploaded and set as a featured image; then automatically set the full size image URL as a custom field. This needs to work on both post Update for existing articles that do not currently have the custom field set and for post Publish for new articles.

As mentioned I have tried numerous suggestions based on this but none seem to work. Here is the last bit of code that I attempted without success though - and the frustrating part is that I cannot seem to debug or get any errors out of this:

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
    $post_thumbnail_id = get_post_thumbnail_id();
    $post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id);
    add_post_meta($post_ID, 'lead_image', $post_thumbnail_url, false);
}

Solutions

The function get_post_thumbnail_id() needs a value to be passed if using outside the wordpress loop. You forgot to pass it.

The code you're using will create a new custom field on each update so here's the modified code that'll perform update_post_meta function if that meta_key is already present in database.

The bellow code is tested on WordPress 3.4.1 with Twenty Ten theme installed

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    $post_thumbnail_id = get_post_thumbnail_id( $post_ID );
        $post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id);

        //to make sure if we already has set the value
        $has_image = get_post_meta($post_ID, 'lead_image', true);

        //will update instead of add if meta_key has value.
        if ($has_image != '') {
            add_post_meta($post_ID, 'lead_image', $post_thumbnail_url, true);
        } else {
            update_post_meta($post_ID, 'lead_image', $post_thumbnail_url, $has_image);
        }
}

Similar questions

Redirect a URL by inserting index.php in between and truncate the url after a string in the redirect url
I have recently ported my blog from one cms to wordpress so I was having trouble in redirect the tag URL which are like this Here "tagname" is the name of tag created using WordPress and "foobar" can be any string I want it to redirect permanently (301) it to such that that string after the tag name i.e "foobar" is truncated and "index.php" is adde...
How To Use YouTube Url Stored In Custom Field To Get Video Image and Set it As Featured Image
I have sourced the following code which captures the URL for a YouTube video placed in the content/body the code then gets the video image from YouTube and sets the image as a featured image. However instead of placing the YouTube URL in the body I'm using a custom field to store the URL. Which can be accessed via the following code How can I modif...
WP dynamic featured image - Can't get second featured image url
I'm actually working on a wordpress website with a Dessign.net theme (the pixel one) which got a beautiful full-page slider on the front page. The slider shows the featured image of selected posts (post for which i've checked "show in slideshow" in the meta box field on edit page). Those featured image are used in the same way for different view on...
Wordpress Setting Featured Image Size and Adding link to featured image and Title
I've created a filterable portfolio in wordpress followindg a quicksand tutorial I found. Everything is working but the author did not link the featured image in the page template nor set the thumbnail size for the post type. I want the thumbnail size to be 280px x 180px. Here is the code: (Edited working code below)
How to remove whole publish block and only show publish button
Using register_post_type i am creating one post_type so in "add new" page right side i don't want whole publish block,i want only publish button. How can i do this.Please help me,My code is - and code to create meta_box is -
Sql Update CPT from publish to draft and particular custom field
I have: CPT: match custom field: played Is it possible by MySql to change the status from publish to draft of all posts of CPT 'match' with custom field set as '0'? A good start can be UPDATE wp_posts SET post_status = 'draft' WHERE post_type = 'match' AND meta_key = 'played' AND meta_value = 0; but meta_key and meta_value are not in wp_posts table...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .