Open basduchambre opened 1 year ago
I did found a workaround, but it requires me to first empty the database with a MySQL query, and then populating the fields using the way WordPress stores information. Not ideal and would be nice if we can get an carbon_set_user_meta()
function.
public function update(\WP_REST_Request $request)
{
global $wpdb;
$parameters = $request->get_params();
$userId = $parameters['userId'];
$workExperience = $parameters['workExperience'];
$query = $wpdb->prepare(
"DELETE from $wpdb->usermeta
where user_id= %d
and meta_key like %s",
$userId,
'%_experience%'
);
if (sizeof($workExperience) === 0) return;
try {
// First clear the database
$wpdb->query($query);
// Set new work experience
for ($index = 0; $index < count($workExperience); $index++) {
$public = $workExperience[$index]['public'] ? 'yes' : 'no';
$currentJob = $workExperience[$index]['currentJob'] ? 'yes' : 'no';
update_user_meta($userId, "_experience|company|$index|0|value", $workExperience[$index]['company']);
update_user_meta($userId, "_experience|position|$index|0|value", $workExperience[$index]['position']);
update_user_meta($userId, "_experience|start-date|$index|0|value", $workExperience[$index]['startDate']);
update_user_meta($userId, "_experience|end-date|$index|0|value", $workExperience[$index]['endDate']);
update_user_meta($userId, "_experience|public|$index|0|value", $public);
update_user_meta($userId, "_experience|current-job|$index|0|value", $currentJob);
}
return wp_send_json([
'message' => 'OK',
], 200);
} catch (\Throwable $error) {
return wp_send_json([
'message' => 'Something went wrong',
'error' => $error
], 500);
}
}
By the way, if you experience the same issue and trying to use this hacky solution, seperate the 'cleaning' of the database to a different method, since I experience that it may interfere with updating the values.
Version
Expected Behavior
Trying to programatically update a complex field for a User. I can update normal fields with
update_user_meta()
using the carbon fields key prepended by an underscore.Actual Behavior
Programatically updating the complex field based on the documentation it should update. However the documentation uses
carbon_set_post_meta()
, but this function does not exists forUsers
. The workaround is using the WordPressupdate_user_meta()
function.Container definition
Method used in class:
Carbon fields container for
user_meta
:Comments
Maybe it is not possible to use
update_user_meta()
for complex fields? I can not find anyone else experiencing this problem. Again: updating simple text fields is no problem.