Add post meta for custom post type with custom fields on publish
-
Hello,
I am trying to trigger a function when a custom post type with custom fields is edit or new. For the custom fields i am using advance custom field plugin. And this is the function and hook
function insert_table_my_products($post_id, $post) { if ($post->post_type == 'my-custom-products') { global $wpdb; global $post; $custom_meta = get_post_meta($post->ID); // echo ('insert message'); //print_r( get_post_meta(get_the_ID(), 'product_code' )); //print_r($custom_meta); //print_r($post); //exit(); if (class_exists('Attachments')) { $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 = 'product_holder.jpg'; } } else { $use_main_image = 'product_holder_no_attach.jpg'; } $wpdb->insert( 'tique_products', array( 'product_code' => $custom_meta['product_code'][0], 'product_name' => $post->post_title, 'product_img_name' => $use_main_image, 'price' => $custom_meta['price_patch'][0], //$POST['acf-field-price_patch'], 'product_inventory' => $custom_meta['stock'][0], //$POST['fields[field_54df75e760b5e]'], ) ); } } add_action('publish_my-custom-products', 'insert_table_my_products', 10, 2);
Main problem i am having the $custom_meta returns empty???
I need all specific post data and custom field data to insert in a custom table
- The topic ‘Add post meta for custom post type with custom fields on publish’ is closed to new replies.