Skip to content

Instantly share code, notes, and snippets.

@psaikali
Created March 4, 2018 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaikali/4f3acaf24f1cc1a79cbda865ebd7b392 to your computer and use it in GitHub Desktop.
Save psaikali/4f3acaf24f1cc1a79cbda865ebd7b392 to your computer and use it in GitHub Desktop.
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