0

I am running this code on the front end to update a post. the post needs be updated multiple times and can be a image or file.

    ...
$call_21 = get_post_meta($call_id, '_call_21', true);
...
        $file_1 = $_FILES['file_1'];
        $new_file_path = $wordpress_upload_dir['path'] . '/' . $file_1['name'];
        $new_file_mime = mime_content_type( $file_1['tmp_name'] );
        while( file_exists( $new_file_path ) ) {
            $i++;
            $new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $file_1['name'];
        }
        // looks like everything is OK
        if( move_uploaded_file( $file_1['tmp_name'], $new_file_path ) ) {
            $upload_id_1 = wp_insert_attachment( array(
                'guid'           => $new_file_path, 
                'post_mime_type' => $new_file_mime,
                'post_title'     => preg_replace( '/\.[^.]+$/', '', $file_1['name'] ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            ), $new_file_path );
            // Generate and save the attachment metas into the database
            wp_update_attachment_metadata( $upload_id_1, wp_generate_attachment_metadata( $upload_id_1, $new_file_path ) );
            // Show the uploaded file in browser
            //wp_redirect( $wordpress_upload_dir['url'] . '/' . basename( $new_file_path ) );

        }
    ...
    $post_id = wp_update_post($post_information);
        if($post_id)
        {
    ...
    update_post_meta($post_id, '_call_21', $upload_id_1);
    ...
    $term_ids = array( 11 );
            $taxonomy = 'call-type';
                wp_set_object_terms( $post_id, $term_ids, $taxonomy );

HTML form code

<input type="file" name="file_1" id="file_1">

The code works great.

The problem

Is that it overrides the meta box entry every time I update the post (like it should be doing) but I need it to do this.

The Desired Result

If the $file_1 is empty $upload_id_1 should keep the value of $call_21

I try this but it is not working.

    if( empty( $file_1 ) ) 
$upload_id_1 = $call_21;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.