Metiers_du_patrimoine_theme/includes/artisans.php
Antoine M dff2a50c35
All checks were successful
continuous-integration/drone/push Build is passing
handling location and date for post title updater
2024-11-22 10:31:08 +01:00

57 lines
1.6 KiB
PHP

<?php
function metiers_patrimoine_artisans_post_updater($post_id)
{
if (!$post_id) return;
$my_post = array();
$my_post['ID'] = $post_id;
$name = get_field("name", $post_id);
$address = get_field("adresse", $post_id);
if (get_post_type() == 'artisans' && $name) {
$my_post['post_title'] = "";
$my_post['post_title'] = $name;
}
if ($address && isset($address['country'])) {
$stateGenericName = getGenericStateNameFromAcfStateName($address['state']) ?? "";
// write_log($stateGenericName);
// Met à jour le champ personnalisé 'country' avec la valeur du pays de l'adresse
update_post_meta($post_id, 'state', $stateGenericName);
}
// write_log($address);
wp_update_post($my_post);
}
add_action('acf/save_post', 'metiers_patrimoine_artisans_post_updater', 20);
function metiers_patrimoine_chantiers_post_updater($post_id)
{
if (!$post_id || get_post_type() !== 'chantiers') return;
$my_post = array();
$my_post['ID'] = $post_id;
$artisan = get_field("artisan", $post_id) ?? "";
$chantier_name = get_field("chantier_name", $post_id) ?? "";
$date = get_field("date", $post_id) ?? "";
$localisation = get_field("localisation", $post_id) ?? "";
if (!$artisan || !$chantier_name) return;
$title = $artisan->post_title . " | " . $chantier_name;
if ($localisation && isset($localisation['city'])) {
$title .= " | " . $localisation['city'];
}
if ($date) {
$title .= " | " . $date;
}
$my_post['post_title'] = $title;
wp_update_post($my_post);
}
add_action('acf/save_post', 'metiers_patrimoine_chantiers_post_updater', 20);