Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enregistrer d'autres champs metas lorsque ACF enregistre un post
<?php
// Article/tutoriel complet sur https://mosaika.fr/astuces-developpement-acf/
// Documentation officielle https://www.advancedcustomfields.com/resources/acf-save_post/
/**
* Enregistrer la variable d'un champ dans un autre champ du post
* Filtre : acf/save_post
*/
function msk_acf_save_another_field($post_id) {
if (get_post_type($post_id) != 'msk_annonce') return;
// Titre (post_title)
$marque = get_field('annonce_marque', $post_id);
$modele = get_field('annonce_modele', $post_id);
$espace = (!empty($marque) && !empty($modele)) ? ' ' : '';
$title = sprintf(
'%1$s%2$s%3$s',
$marque,
$espace,
$modele
);
wp_update_post(
[
'ID' => $post_id,
'post_title' => $title
]
);
// Prix (meta annonce_prix_avec_commission)
$prix = (float)get_field('annonce_prix', $post_id);
if ($prix > 0) {
$prix_commission = round($prix * 1.25, 2);
update_field('annonce_prix_avec_commission', $prix_commission, $post_id);
}
}
add_action('acf/save_post', 'msk_acf_save_another_field', 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment