Skip to content

Instantly share code, notes, and snippets.

@joniahola
Created February 23, 2021 16:02
Show Gist options
  • Save joniahola/5abb2faf79a28ec7eb4b14ccd39a7fbb to your computer and use it in GitHub Desktop.
Save joniahola/5abb2faf79a28ec7eb4b14ccd39a7fbb to your computer and use it in GitHub Desktop.
Create translations to original post (duplicate data). (Wordpress + Polylang)
add_filter('acf/save_post' , function($post_id) {
//only want to modify events
if(get_post_type($post_id) === 'event') {
$post = get_post($post_id);
$langs = ['en', 'fi', 'sv'];
$currentLang = pll_current_language();
$pagetranslations = [];
for($i = 0; $i < count($langs); $i++) {
if($langs[$i] == $currentLang) {
unset($langs[$i]);
break;
}
}
//use same data to all languages
$postData = [
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'post_status' => $post->post_status,
'post_type' => $post->post_type,
'post_author' => $post->post_author
];
foreach($langs as $l) {
$pid = wp_insert_post($postData);
pll_set_post_language($pid, $l);
$pagetranslations[$l] = $pid;
}
//also need to add original post to pageTranslations array
pll_set_post_language($post_id, $currentLang);
$pagetranslations[$currentLang] = $post_id;
pll_save_post_translations($pagetranslations); //complete!
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment