Skip to content

Instantly share code, notes, and snippets.

@zArubaru
Created February 13, 2018 10:24
Show Gist options
  • Save zArubaru/4f4080c5fe74c3b94e8512a08698e9a7 to your computer and use it in GitHub Desktop.
Save zArubaru/4f4080c5fe74c3b94e8512a08698e9a7 to your computer and use it in GitHub Desktop.
Resync WordPress posts' taxonomy translations
<?php
/**
* "Resync" WP posts' taxonomy translations.
*
* This can either run after the `php` command,
* followed with CTRL+D x2 (OR x3), or included
* as a file in `php -a` interactive mode:
*
* https://jondavidjohn.com/interactive-wordpress-php-shell/
*/
define('CLI_WP_ROOT', dirname(__FILE__));
// Change this to your WP site url
$_SERVER['HTTP_HOST'] = 'https://www.website-address.com/';
ob_start();
require_once(CLI_WP_ROOT . '/wp-load.php');
require_once(CLI_WP_ROOT . '/wp-admin/includes/admin.php');
ob_end_clean();
$admin_sync = new PLL_Admin_Sync($polylang);
$src_lang = 'fi'; // change language as needed
$target_langs = ['en']; // change languages as needed
$posts = get_posts([
'posts_per_page' => PHP_INT_MAX,
'lang' => $src_lang,
]);
foreach ($posts as $a_post) {
foreach ($target_langs as $target_lang) {
$trans_post_ID = pll_get_post($a_post->ID, $target_lang);
if ($trans_post_ID) {
$admin_sync->copy_taxonomies($a_post->ID, $trans_post_ID, $target_lang);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment