Should I use add_action(‘publish_post or add_filter(‘publish_post?

Question

I am trying to trigger an extra function as soon as a certain custom post type is published

function insert_table_products($post_id, $post) {
    if ($post->post_type == 'custom-products') {
        global $wpdb;
        $custom_meta = get_post_meta($post_id);
        //print_r($custom_meta);
        //print_r($post);
        $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();
        endif;

        $wpdb->insert(
                'products', array(
            'product_code' => $custom_meta['product_code'][0],
            '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]'],
                )
        );
    }
}
add_filter('publish_post', 'insert_table_products', 10, 2);
//add_action( 'publish_post', 'insert_table_products', 10, 2);

Neither of the hooks work. I expected the print_r to show some data

BTW, I have also a update function, triggered by

//add_filter('edit_post', 'update_table_products', 10, 2);

which also doesn’t seem to work correctly.

Any ideas?

0
Alex 8 months 2020-12-16T16:10:52-05:00 0 Answers 5 views 0

Leave an answer

Browse
Browse