update_post_meta() data appears to be overwritten with NULL
-
I have a custom post type, with several custom meta values. ‘Add’ and ‘update’ for this CPT are working fine from the Admin UI.
Now I need to import multiple records from an external source, and I have an external script which POSTs to accomplish this.
I have tested many times POSTing a single record from my external script and it appears to be working as expected. i.e. record is created if it does not exist, meta values are added and values are set correctly. Repeating this one single POST shows update_post_meta() returning false in my debugging as expected (since values already exist), and the meta values continue to appear correctly in the MySQL database and Admin UI. However..
Here is my problem: When I invoke my external script with two or more CPT updates in two POSTs, previously correct values for all all except the last update appear empty in the Admin UI, and NULL in the database.
My external script sends stringified json in the POST so I have this:
if ($_SERVER['REQUEST_METHOD'] == "POST"){ $json = file_get_contents('php://input'); if ($json){ $data = json_decode($json, true); $registry = new My_Registry($data); $registry->create_or_update(); } }
-and here is the create_or_update() method that is called:
function create_or_update(){ $post_id = $this->_exists(); if ( ! $post_id ){ $post_id = wp_insert_post(array ( 'post_type' => 'gwc_registry', 'post_title' => $this->title, 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed' )); } if ( $post_id ){ $this->registry_cpt_id = $post_id; } $this->_update_meta(); }
_exists() returns false or the post_id if found (I’m trying to keep code lines down, but can share if helpful).
Here is the essence of _update_meta()
private function _update_meta(){ $cpt_meta_fields = (array)$this; foreach ($cpt_meta_fields as $key => $value){ $clean_key = (explode("\0", $key))[2]; $registry_meta_key = 'registry_' . $clean_key; var_dump($registry_meta_key . " > " . $value); //**see comment below //....// update_post_meta( $this->registry_cpt_id, $registry_meta_key, $value ); } }
The debug line at ** always shows correct meta data right before the call to update_post_meta() in my testing, but when I post two records from my external script the first one still gets overwritten with NULLS as described at the top.
This is not a timing issue – I have tried putting a delay in my external script, and I can watch the correct meta data get written into the database for the first record and subsequently get set to NULL when the second record gets processed – even though update_post_meta() returns false for that ‘nullified’ record at the same time!
This appears to be something in the way I am using update_post_meta here but I am at a loss as to how to proceed, and would really appreciate any hints as to where I might dig next.
Thank you in advance for any advice..
- The topic ‘update_post_meta() data appears to be overwritten with NULL’ is closed to new replies.