wordpress wp_include_post() in custom post with coustom field

109 views Asked by At

I'm new in WordPress and my English are not good. I'm using Custom Post Type UI plugin to create a post type Product and Advanced Custom Fields plugin to create some custom fields like address. Now I'm reading XML file (title, description, price, brand etc) with 3000 products and save all in my custom database. While reading and save it in database I want to create post like product where name is title, slug is title (can I create a category by brands or PARENTpage from other custom field?) description etc. With wp_insert_post() how can I insert $variable in custom field? I want something like

$my_post = array(
  'post_title'    => $row['product_name'],
  'post_content'  => $row['description'],
  'post_status'   => 'publish',
  'post_name'      => $row['product_name'],
  'post_author'   => 1,
  'post_type'     => 'product',
  'custom_field_brand'         => $row['brand'], 
  'custom_field_price'         => $row['price']
);

wp_insert_post( $my_post );
1

There are 1 answers

0
Bhargav On BEST ANSWER

Advanced Custom Fields saves the post meta a little differently. WHat you have to do here is instead of including the post meta in the wp_insert_post after

wp_insert_post( $my_post );

returns the ID of the post created.

Use the Post ID with the acf function update_field() to update the post meta like this

update_field( custom_field, custom_field_data, $post_id );