So I have made some kind of importer(From another non WP db to my WP db) which handles everything in a click of a button.
Now it does correctly create a post type although one of the custom fields made with ACF in which I have to place the ID from the other DB is not filling in correctly and I did this according to update_post_meta
. Now I have no idea what the issue is because when I look into my WordPress DB it shows that it adds it correctly into the database but it does not add(Better said Show) WITH the custom post in the WP-Admin.
This is the code i’ve used:
<form action="" method="post">
<input type="submit" value="Test" id="submit" name="submit">
<?php
if(isset($_POST['submit'])){
foreach ($result as $c) {
$postargs = array(
'post_title' => $c->member_name,
'post_status' => 'draft',
'post_type' => 'bedrijf'
);
$thisid = wp_insert_post ( $postargs, true);
if ( is_wp_error($thisid) ) {
return get_error_codes();
} else {
update_post_meta( $thisid, $meta_key, $c->memberID);
}
}
}
?>
</form>
A screenshot from my DB showing that it does add(Yes the meta key is correct as I only have one custom field). The meta value is also correct as I put it on LIMIT 1 in the query I have so it shows the first value of the DB only.
EDIT: To be exact it adds it in the wp_postmeta
table.
Found the solution in this post:
update_post_meta and update_field ony working when saving the post
The solution is to use
update_field
becauseupdate_post_meta
uses a different method to update fields than ACF does. Do note that this is the correct order of using the codeupdate_field($meta_key, $meta_value, $post_ID);
.