• I am trying to get the featured image url after a post is published but somehow it returns an empty URL.

    But if I was to get the post_title, it is ok.

    function update_map($post_id) {

    global $wpdb;

    $thumbnail_id = get_post_thumbnail_id($post_id);
    $url = wp_get_attachment_url($thumbnail_id);

    if (!empty($url)) {
    wp_mail(‘[email protected]’, ‘Yes’, ‘Yes’);
    }

    wp_mail(‘[email protected]’, ‘No’, ‘No’);
    }

    add_action(‘publish_post’, ‘update_map’, 50);
    If I was to update this same post with the save_post hook

    add_action(‘save_post’, ‘update_map’, 50);
    in the WordPress editor after publishing, I am able to get the featured image url again. Am I missing anything?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter soma2000

    (@soma2000)

    function update_map($post_id) {

    global $wpdb;

    $thumbnail_id = get_post_thumbnail_id($post_id);
    $url = wp_get_attachment_url($thumbnail_id);

    if (!empty($url)) {
    wp_mail(‘[email protected]’, ‘Yes’, ‘Yes’);
    }

    wp_mail(‘[email protected]’, ‘No’, ‘No’);
    }

    add_action(‘publish_post’, ‘update_map’, 50);`

    Hope that code above is better for reading.

    Moderator bcworkz

    (@bcworkz)

    The featured image is not really part of a post object, it is meta data. ‘publish_post’ fires before meta data is saved. ‘save_post’ fires after meta data has been saved. The post title is part of the post object, so works with either action.

    Thread Starter soma2000

    (@soma2000)

    Is there a work around? Any suggestion that I could try?

    I am pretty new to wordpress hack, only write some code when I need some features to work.

    Thx,

    Thread Starter soma2000

    (@soma2000)

    @bcworkz

    Is there a work around that I could use? I need it for ‘publish_post’ to work for featured image url.

    Thx,

    Moderator bcworkz

    (@bcworkz)

    It appears my information about what happens when is outdated. I don’t really see why ‘publish_post’ won’t work. Be sure you have updated to the latest version of WP before trying anything else.

    I think the following logic will work regardless:
    In the ‘publish_post’ callback add an action hook to ‘save_post’ that does what you need. When that is done, before returning, have the current ‘save_post’ callback remove itself as a hook.

    Another approach would be to go ahead and only hook ‘save_post’. Check the status and post type of the passed post object. If they are both true, then it’s a reasonable assumption that ‘publish_post’ had fired, even if you have not hooked it.

    I see you got fooled into double posting by the forum’s occasionally wonky caching. FYI, if you wait a minute or two, then reload, the post that seemed to be lost magically appears ??

    Thread Starter soma2000

    (@soma2000)

    The entire add action called is as shown below.

    function update_google_map($post_id) {
    
        // If this is just a revision, don't send the email.
    
        global $wpdb;
    
        $post_title = get_the_title($post_id);
    
        $post_content1 = get_post_field('post_content', $post_id);
        $post_content = substr($post_content1, 0, 150);
    
        $post_url = get_permalink($post_id);
    
        $feat_image_url = wp_get_attachment_url(get_post_thumbnail_id($post_id));
    
        $google_location = get_post_meta($post_id, 'g_location', $single = true);
    
        $google_loc = explode(",", $google_location);
    
        $chk_result = $wpdb->get_results("SELECT post_id FROM wp_wpgmza WHERE post_id = '" . $post_id . "'");
    
        if (count($chk_result) > 0) {
            $wpdb->update(
                    'wp_wpgmza', array(
                'description' => $post_content,
                'pic' => $feat_image_url,
                'link' => $post_url,
                'lat' => $google_loc[0],
                'lng' => $google_loc[1],
                'title' => $post_title,
                'map_id' => 1
                    ), array('post_id' => $post_id), array(
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%d'
                    )
            );
        } else {
            $wpdb->query("INSERT INTO wp_wpgmza (post_id, map_id, description, pic, link, lat,lng, anim, title, infoopen) "
                . "VALUES ('" . $post_id . "', 1, '" . $post_content . "', '" . $feat_image_url . "', '" . $post_url . "', '" . $google_loc[0] . "', '" . $google_loc[1] . "', 0, '" . $post_title . "', 0)");
        }
    
    }
    
    add_action('publish_post', 'update_google_map', 50);

    So both the $feat_image_url and $google_location returned empty string. The rest such as the $post_title and $post_url are ok.

    Now after each publish, I simply go to the wordpress editor and click on update post and both the $feat_image_url and $google_location will be correctly inserted into wp_wpgmza.

    I will see how I could implement your suggestions….

    Thx,

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Getting featured image url after publishing’ is closed to new replies.