• Thu. Dec 9th, 2021

custom post types – retrieve Meta field value from CPT and set those values as Image Meta Field “Title”

Byadmin

Nov 25, 2021


So i have a created a CPT (Car) with custom meta fields (Model, Maker, car photo ..etc) and those meta fields are being filled using a frontend form , all these has been created using JetEngine Plugin.

My issue is i want to auto fill Photo Meta (Title) using two Meta fields value

for Example:
user fill up a form

maker = BMW

model = X6

photo = .jpg

i want the uploaded Photo to have the Maker and Model as a title of the photo.

hopefully i could explain this well if not please check the screenshot below

also this my starting 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

Leave a Reply

Your email address will not be published. Required fields are marked *