Wednesday, December 8th, 2021

Custom Post Type – retrieve meta field values ​​from cpt and set those values ​​as image meta field “title”

So I have created a CPT (Car) with custom meta fields (Model,Maker,CarPhoto..etc) and those meta fields are being filled using frontend form, all of them created using jetengine plugin Is.

My problem is that I want to auto-fill the photo meta (title) using two meta field value

For example: User fill out a form

Manufacturer = BMW

model = X6

photo = .jpg

I want the uploaded picture to have the maker and model as the title of the photo.

hope i can explain it well if not please see below screenshot

also my initial code

add_action( 'save_post_car', 'wpse_name_set_slug', 10, 2 );

function wpse_20151219_after_post_meta($meta_id, $post_id, $meta_key, $meta_value) {

    if($meta_key === '_wp_attachment_metadata') {

       $meta_value = array(
        get_post_meta( $post_ID, 'maker-meta-field', true ),
        get_post_meta( $post_ID, 'model-meta-field', true ),
    );


        $meta_value[ 'image_meta' ] = array_merge($meta_value[ 'image_meta' ], array(
            'title'     => "This is a META TITLE for $post_id",
        ));

        // Update The Image Metadata
        wp_update_attachment_metadata($post_id, $meta_value);

        $attachment_meta = get_post_meta($post_id);

        $attachment_metadata = wp_get_attachment_metadata($post_id);
    }
}

Source link