user3528954 user3528954 - 4 years ago 103
PHP Question

How to get featured image from post

Currently working on a

blog site

what I'm trying to do is I want to call a functions when a post is publish for a first time not on update of post and try to retrieve featured
image url
that is attached to that post.

I want to know which hook I have to call and how to do that. Do I have to add plugins?.

Answer Source

save_post hook can be used for this purpose it has a third parameter $update which can be used to determine just to check it is new or existing.

 * Save post metadata when a post is saved.
 * @param int $post_id The post ID.
 * @param post $post The post object.
 * @param bool $update Whether this is an existing post being updated or not.
function save_post_first_time($post_id, $post, $update)
    //check for revision
    if (wp_is_post_revision($post_id))
    if (!$update)
        // it's a new post
        //getting post featured image ID
        $post_thumbnail_id = get_post_thumbnail_id($post_id);
        $imageSRC = wp_get_attachment_image_src($post_thumbnail_id, 'thumbnail');
        // it's an existing post

add_action('save_post', 'save_post_first_time', 10, 3);


Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download