Support

Account

Home Forums Backend Issues (wp-admin) Save ACF fields in a Custom Post Type in a custom db table

Helping

Save ACF fields in a Custom Post Type in a custom db table

  • Hello,

    I am using your excellent plugin to add some custom fields to a CPT. But i am having trouble with saving data to a custom table.

    i have tried:

    add_action('transition_post_status', function ( $new_status, $old_status, $post ) {
    
        if ('publish' == $new_status && 'publish' != $old_status && $post->post_type == 'my-custom-products') {
    
            //DO SOMETHING IF A POST IN POST TYPE IS EDITED
                global $wpdb;
    
                $custom_meta = get_post_meta($post->ID);
                if (isset($custom_meta['product_code'][0])) {
                    $attachments = new Attachments('my_attachments');
                    if ($attachments->exist()) {
                        $my_index = 0;
                        $use_image = new SplFileInfo($attachments->url($my_index));
                        $use_main_image = $use_image->getFilename();
                    } else {
                        $use_main_image = '';
                    };
    
                    $wpdb->update(
                            'tique_products', array(
                        'product_name' => $post->post_title,
                        'product_img_name' => $use_main_image,
                        'price' => $custom_meta['product_price'][0], //$POST['acf-field-price_patch'],
                        'product_inventory' => $custom_meta['product_stock'][0], //$POST['fields[field_54df75e760b5e]'],
                            ), array('product_code' => $custom_meta['product_code'][0]), array(
                        '%s', '%s', '%s', '%d'
                            )
                    );
                }//end if exits
        }
    }, 10, 3);

    also tried to trigger the function with
    add_action('save_my-custom-products'

    But the $custom_meta (custom fields, i.e. ACF) reads always empty??

    How do i fix this??

  • Hi @ddt,

    Thanks for the post.

    In this implementation I would recommend a slightly different workaround whereby you would need to obtain the custom field values by hooking into the acf/save_post action before ACF does, and saving the $_POST data in a custom table, then clearing the $_POST data to prevent ACF from saving it.

    I hope this info helps.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.