Hi,
I have an issue in connection with a support ticket https://toolset.com/forums/topic/saving-image-field-as-featured-image/ applied a few days back.
Initially, it was working fine in terms of featured images of Companies post attached to Jobs post(Related post). But nowadays, things are behaving inconsistently.
The trick works in some of the posts while it misses for the others. I don't know why? I have tried by deactivating plugins too but not working. Please help.
Here are example URLs of the issue:
1) hidden link (showing featured image of the related company post)
2) hidden link (not showing the featured image of the related company post)
I took another look at this and found that the issue was that the URL's weren't getting converted to their ID's which causes the function that is setting the thumbnail for the post to fail.
I've since replaced this with a custom function below to help fix this.
// Put the code of your snippet below this comment.
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
// Add Shortcode
function custom_shortcode() {
$child = get_the_ID();
$parent = toolset_get_related_post( $child, "company-job" );
$featured = get_post_meta($parent,'wpcf-company-image');
set_post_thumbnail($child,pippin_get_image_id($featured[0]));
}
add_shortcode( 'parent_id_shortcode', 'custom_shortcode' );
Thanks, Shane for your support. Hope things are working fine. I am keeping one day for testing so that I re-ensure that no problem persists in this regard. By the way, should I remove the shortcode [parent_id_shortcode] in the Job template? Besides that should I take any other action in this regard?
Given that not all your posts have not yet gotten the image added to the child posts I would suggest that you keep the [parent_id_shortcode] shortcode in the content template for now.
Once all the items have been copied over you can then go ahead and remove it but it will take some time because each post will need to be visited individually at least once.
I took a look at this and I would suggest re-uploading the image. Delete the original image from the parent and re-upload it.
The problem that i'm seeing is that none of the methods that wordpress provides is receiving the ID of the image. The Image ID is needed in order to use the default function to set the featured image on the child post.
If i'm unable to get the ID of the image then I can't copy it onto the child post. I tried this by using a different image in the same post parent and it was copied to the child successfully.