Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

Reply To: Featured Image

$
0
0

I assume you are going to do this:
1) you are using a custom post type, slug is "my-cpt", and a custom image field "main-image", created with Types, and set it as a required field
2) when user create/update the post, they must upload/choose a image, you are going to set "main-image" image as the post "featured image"(Post_Thumbnails):
http://codex.wordpress.org/Post_Thumbnails

Please try WP action hook “save_post”, like this:

function my_featured_image_func( $post_id ) {

	// If this is just a revision, don't do anything.
	if ( wp_is_post_revision( $post_id ) || get_post_type($post_id) != 'my-cpt' )
		return;
	global $wpdb;
	$main_image = get_post_meta( $post_id, 'wpcf-main-image', true );
	if($main_image){
		$attachment_id = $wpdb->get_var($wpdb->prepare(
			"SELECT ID FROM $wpdb->posts WHERE guid = %s",
			$main_image));
		update_post_meta($post_id, '_thumbnail_id', $attachment_id);
	}
}
add_action( 'save_post', 'my_featured_image_func' );

Please replace "my-cpt" with your custom post type slug, and replace "main-image" with your custom image field slug
More help:
http://codex.wordpress.org/Plugin_API/Action_Reference/save_post


Viewing all articles
Browse latest Browse all 20145

Trending Articles