Skip Navigation

[Resolved] Setting Featured Image using Custom Field

This support ticket is created 3 years, 6 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.

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

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 3 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2097987

Tell us what you are trying to do?
I have 400+ posts only some of which have a Featured Image but they all have the 'media-image' ([types field='media-image' title='%%TITLE%%' alt='%%ALT%%' ][/types]) field. I would like to set the Featured Image to the 'media-image' field entry if a Featured Image does not already exist.

Is there any documentation that you are following?
I have found very old entries about this issue but hopefully it has been resolved by now.

Is there a similar example that we can see?
No.

What is the link to your site?
Archive listing: hidden link which needs the Featured Image
Single listing that shows image using the 'media-image' versus Featured Image: hidden link

Thank you.

#2098817

Hi,

Thank you for contacting us and I'd be happy to assist.

If your goal is to show the image from the custom field 'media-image', when no featured image exists, you can use the conditional output for this:
https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/

For example:


[wpv-conditional if="( '[wpv-post-featured-image output="url"]' ne '' )"]
[wpv-post-featured-image size="full"]
[/wpv-conditional]

[wpv-conditional if="( '[wpv-post-featured-image output="url"]' eq '' )"]
[types field='media-image' title='%%TITLE%%' alt='%%ALT%%' ][/types]
[/wpv-conditional]

The first conditional block checks if the featured image exists and if it is true, it shows the featured image.

The second one checks if the featured image doesn't exist and if it is true, it shows the image from the 'media-image' field.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2099201

Thank you Waqar but my goal isn't to display the image, it is to set the Featured Image to the 'media-image' entry. Using code I want to make it so whatever image is in the 'media-image' custom field that image will now be attached to the Featured Image.

Thanks,
Susan

#2102075

Thank you for waiting.

To programmatically update the featured image (for posts where it doesn't already exist), from an image type custom field, you'll need some custom code:


add_shortcode('custom_featured_image_conversion', 'custom_featured_image_conversion_func');
function custom_featured_image_conversion_func($atts) {

	$post_type = $atts['type'];
	$field = $atts['field'];

	// get all the target posts
	$args = array(
		'post_type'        => $post_type,
		'posts_per_page'   => -1,
		'post_status'      => get_post_stati(),
		);

	$posts_array = get_posts( $args );

	foreach ($posts_array as $post) {
		// if no featured image is set
		if( !(has_post_thumbnail($post->ID)) ) {
			// get image URL from field
			$field_image_url = types_render_field( $field, array( 'item' => $post->ID, 'output' => 'raw' ) );
			// if image exists, set it as a featured image
			if(!empty($field_image_url)) {
				$image_id = attachment_url_to_postid($field_image_url);
				if($image_id > 0) {
					set_post_thumbnail( $post->ID, $image_id );
				}
			}
		}
	}

}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please create a temporary page and insert this new custom shortcode like this:


[custom_featured_image_conversion type='book' field='media-image']

Note: in this example, we assume that the target post type is "book" and the target image custom field to use as a source is "media-image".

After saving the page, visit it on the front-end so that the code can execute in the backend. The custom code will cycle through all the 'book' type posts, and if a featured image doesn't exist and the image is available in the 'media-image' field, it will attach it as a featured image too.

Once, the code has been executed, you can delete this temporary page.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/