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_google_map($post_id) {
// If this is just a revision, don't send the email.
global $wpdb;
if (wp_is_post_revision($post_id))
return;
$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);
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. The same also apply to $google_location. It is also empty when it tries to retrieve from postmeta table.
Am I missing anything?
Please replace this code that you have used and check you have got feature image as
publish_post
Because
$url
is not an array it’s string so you can’t use!empty
Try using the “wp_after_insert_post” hook.
}