Last active
October 23, 2019 20:56
-
-
Save isotrope/211b7b94b253496a51f075f1dae24203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$post_id = get_the_ID(); // ou whatever fonction pour avoir ton post ID | |
/* | |
* Les arrays sont très pratiques pour traverser des clés aussi | |
* | |
* Trouve les termes qui sont pareils dans les deux cas, | |
* on va concatener post_ ou live_ plus tard | |
*/ | |
$pairs = [ | |
'name', | |
'description', | |
'url', | |
'other_key', | |
'yet_another_key', | |
'a_repeater_key' => [ | |
'product_img', | |
'product_url', | |
], | |
'mo_keys_mo_problems', | |
]; | |
foreach ( $pairs as $pair ) { | |
// trouve tes deux valeurs | |
if ( function_exists( 'have_rows' ) && is_array( $pairs[ $pair ] ) ) { | |
// code pour repeaters | |
if ( have_rows( $pair ) ) { | |
while ( have_rows( $pair ) ) { | |
the_row(); | |
// pour chacun des sous-champs sur CETTE rangée | |
foreach ( $pairs[ $pair ] as $pair_key ) { | |
$post_value = get_sub_field( 'post_' . $pair_key ); | |
$live_value = get_sub_field( 'live_' . $pair_key ); | |
if ( $post_value != $live_value ) { | |
update_sub_field( 'live_' . $pair_key, $post_value ); | |
} | |
} | |
} | |
} | |
} else { | |
// code pour champ simple | |
$post_value = get_post_meta( $post_id, 'post_' . $pair, true ); | |
$live_value = get_post_meta( $post_id, 'live_' . $pair, true ); | |
// si elles sont différentes, | |
if ( $post_value != $live_value ) { | |
// mets-les à jour | |
if ( function_exists( 'update_field' ) ) { | |
// ACF style (avec la détection de la fonction (function_exists() ) | |
update_field( 'live_' . $pair, $post_value, $post_id ); | |
} else { | |
// Old school | |
update_post_meta( $post_id, 'live_' . $pair, $post_value ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment