Update a JetEgine repeater field in Custom Content Type using PHP code(CCT)
$cct_slug = "test"; // Name of the Custom Content Type. Don’t add “jet-cct-“ as JetEngine will add it by default.
$cct_id = 10; //The ID of the Custom Content Type item you want to update
$content_type = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types(
$cct_slug
);
$repeater_field = "repeater_field_name";
$repeater_field_meta1 = "repeater_field_meta1";
$repeater_field_meta2 = "repeater_field_meta2";
// Replace the actual field values
$repeater_field_value = [$repeater_field_meta1 => "repeater_field_meta1_value",
$repeater_field_meta2 => "repeater_field_meta2_value"];
$new_data = [$repeater_field => [$repeater_field_value]];
$where = ["_ID" => $cct_id];
if (!$content_type) {
return;
}
$limit = 1;
$offset = 0;
$order = [["orderby" => "_ID", "order" => "desc"]];
$eentries = $content_type->db->query(
$where,
$limit,
$offset,
$order
);
$eentry = !empty($eentries[0]) ? $eentries[0] : false;
// Append the new value to the repeater field
$repeater_field_values = isset($eentry[$repeater_field])? $eentry[$repeater_field] : [];
if (empty($repeater_field_values)) {
$new_data = [$repeater_field => [$repeater_field_value]];
$content_type->db->update($new_data, $where);
} else {
array_push($repeater_field_values,$repeater_field_value);
// Update the database with the new data
$new_data = [$repeater_field => $repeater_field_values];
$content_type->db->update($new_data, $where);
}
Post Views: 383