on save post using CPT resets my CF

I have an issue with my wordpress

I’ve created CPT (custom post type), which creates product.
For every CPT I’ve related another CPT which let me add pictures gallery and CF (custom fields), which letting me list icons, that should display on tick checkbox.

Read More

Talking about my function…. on save_post function should post_parent ID for specific post and save data.
For the first sight it looks ok, but in moment I’m trying to add related CPT with images on save post it reset my ticked Custom fields with list of icons(slug produkt-icons).

so, samples below

1) I add post, tick the icons, not adding any CPT with images = save post and icons (but I don’t have images 🙁 )

2) I add post, tick the icons, add images from CPT = ticked checkboxes reset their values, but post and images displays 🙁

Any ideas? Thanks for help 🙂

Function below

add_action( 'save_post', 'parent_page_for_post_type', 10, 3 );

function parent_page_for_post_type( $post_id ) {

    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
    $current_post_type = get_post_type();

    // save posts for parent_page IDs
    if ( $current_post_type == 'post' ) {

    set_parent_for_post_func( $post_id, 7);
    }
    elseif ( $current_post_type == 'produkt' ) {
        set_parent_for_post_func( $post_id, 32);
    }
}

function set_parent_for_post_func($post_id, $my_post_id) {
// If this isn't a 'post' post, don't update it.
    foreach((array)get_post_type($post_id) as $post_type) {

    remove_action( 'save_post', 'parent_page_for_post_type' );
    // update the post, which calls save_post again

    $my_post = array ();
    $my_post['ID'] = $post_id;

    $my_post['post_parent'] = $my_post_id;

    wp_update_post( $my_post );
    // re-hook this function

    add_action( 'save_post', 'parent_page_for_post_type' );
    }
}

Related posts