Skip Navigation

[Resolved] Short Code behaving inconsistently

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by deepS 3 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1762207

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.

#1762349
Screenshot_1.jpg

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)

#1762527

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for getting in touch.

Could you allow me to have admin access to the website so that I can have a look to see what is the difference between these 2 pagees?

The private fields will be enabled for your next response.

Thanks,
Shane

#1762699

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Not sure why the code wasn't working but from what I can see it should be working fine.

Is there any other post that this issue is occurring on ?

Thanks,
Shane

#1762731

Yah there are other pages too like:

hidden link

hidden link

hidden link

hidden link

Hope these would help you find the problem.

#1762817

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

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' );

The above is the updated function.

Please let me know if this helps.
Thanks,
Shane

#1763057

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?

Thanks.

#1763473

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

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.

Thanks,
Shane

#1765597

Hi Shane,
After testing your code for two days I find one url where the image is not appearing. The url is hidden link

Please check if any modification is required. Thanks.

#1765605

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

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.

Thanks,
Shane

#1768189

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.