Auto set Featured Image from Relationship Media Field
-
I’m trying to automatically set Featured Image upon saving/updating custom post type. Image should come from Relationship Media Field where user can upload pictures. It should set 1st image uploaded as a feature image.
Below code doesn’t work and I’m not sure why.
Custom post type name: car_listing
Relationship Media field: imagesadd_action( 'pods_api_post_save_pod_item_car_listing', 'slug_auto_featured_img', 10, 3 ); function slug_auto_featured_img( $pieces, $is_new_item, $id ) { //get the values of the 'images' field $imgs = $pieces[ 'fields' ][ 'images' ][ 'value' ]; //get ID of first index in array $img = $imgs[0][ 'ID' ]; //if there is nothing there set $img to null to avoid errors if ( empty( $img ) ) { $img = null; } //Prepare to update the post's featured image. $my_post = array( 'ID' => $id, 'post_thumbnail' => $img, ); //Update the post wp_update_post( $my_post ); }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Auto set Featured Image from Relationship Media Field’ is closed to new replies.