0

I am trying to insert data into wp_post and wp_postmeta tables but it seems like i am doing something wrong.

Here is my code:

$post_data = array(
                                    'post_title' => $data[0],
                                    'post_content' => $data[1],
                                    'post_status' => 'publish',
                                    'post_type' => 'product'
                                );

                                $id = wp_insert_post($post_data);
                                add_post_meta($id, '_stock', $data[6]);
                                add_post_meta($id, '_price', $data[7]);

The post is inserted as it should but when i check the postmeta table no values had been inserted. I also tried the update_post_meta but still doesn't work.

Any suggestions?

  • It should work. Turn WP_DEBUG on and check if there are any warnings or notices. You can also var_dump $id variable to check if it is post id and not WP_Error... – Krzysiek Dróżdż Jun 19 '13 at 11:29
1

What user34296 said is update_post_meta() will update the value of the existing meta key (custom field) for the specified post. If it does not already exist it will call add_post_meta($post_id, $meta_key, $meta_value) instead. That's why you should use it.

EDIT: in this case it should work, error is coming from another part of the code.

Source

0

you should try like this:

update_post_meta( $id, '_price', true );
  • 1
    Please add an explanation to your answer: why could that solve the problem? – fuxia Jun 19 '13 at 12:04
  • sorry,i'm not english man.just know it will work.. – moyu Jun 21 '13 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.