I have a sample code to create a Post of a specific type (product of WooCommerce
) and would like to use it to do a routine importing many products.
<?php
function createNewProduct() {
$new_post = array(
'post_title' => "Custom Variable Test",
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_type' => 'product'
);
$skuu = 'custom-prod-1';
$post_id = wp_insert_post($new_post);
echo 'post_id = ' . $post_id;
update_post_meta($post_id, '_sku', $skuu );
update_post_meta( $post_id, '_price', "25" );
wp_set_object_terms( $post_id, 'Gasolina', 'pa_fuel' );
wp_set_object_terms( $post_id, 'Ford', 'pa_brand' );
update_post_meta( $post_id, '_visibility', 'search' );
update_post_meta( $post_id, '_stock_status', 'instock');
// Lendo o Post criado
$content_post = get_post($post_id);
$content = $content_post->post_content;
// $content = apply_filters('the_content', $content);
// echo '<pre>' . print_r($content, true) . '</pre>';
// echo '<pre>' . print_r($content_post, true) . '</pre>';
}
?>
I'd like to know the best approach for importing the data. When I put this routine in a plugin and Enabled by the visual interface Wordpress runs the code countless times creating repeated records. When I run the PHP file in isolation, it reports error Fatal error: Call to undefined function
.
Any good practice tips to use for this type of problem?