Skip to content

Instantly share code, notes, and snippets.

@gpbeer
Last active April 3, 2020 08:45
Show Gist options
  • Save gpbeer/1837d730318aa299c13892b4ea2aea46 to your computer and use it in GitHub Desktop.
Save gpbeer/1837d730318aa299c13892b4ea2aea46 to your computer and use it in GitHub Desktop.
Migrate terms from old taxonomy to new taxonomy and assign respective posts.
<?php
/**
* Go to /wp-admin/edit-tags.php?taxonomy={initial_tax}&post_type={post_type} and paginate in categories to execute script.
* You can delete this file once it's done
* Class Migrate_Category
*/
class Migrate_Category
{
const TAXONOMY_COLUMN = 'data'; // Column name
/**
* @var string
*/
protected $initial_tax;
/**
* @var string
*/
protected $target_tax;
/**
* @var string
*/
protected $post_type;
/**
* @var WP_Term
*/
private $cloned_term;
/**
* Migrate_Category constructor.
*
* @param $initial_tax
* @param $target_tax
* @param $post_type
*/
public function __construct($initial_tax, $target_tax, $post_type)
{
$this->initial_tax = $initial_tax;
$this->target_tax = $target_tax;
$this->post_type = $post_type;
add_action('manage_edit-category_columns', [$this, 'add_column'], 1);
add_filter("manage_category_custom_column", [$this, 'column_content'], 10, 3);
}
/**
* Column is used to get access to term_id and output data
*
* @param $columns
*
* @return mixed
*/
public function add_column($columns)
{
$columns[self::TAXONOMY_COLUMN] = self::TAXONOMY_COLUMN;
return $columns;
}
/**
* @param $value
* @param $column_name
* @param $id
*/
public function column_content($value, $column_name, $id)
{
$reset = false;
if ($column_name == self::TAXONOMY_COLUMN) {
// Delete new terms in dev
if ($reset) {
$terms_delete = get_terms([
'taxonomy' => $this->target_tax,
'hide_empty' => false,
]);
if ($terms_delete) {
foreach ($terms_delete as $term_n) {
if (term_exists($term_n->term_id, $this->target_tax)) {
wp_delete_term($term_n->term_id, $this->target_tax);
}
}
}
// Cache entries
delete_option($this->target_tax . '_children');
return;
}
$term = WP_Term::get_instance($id, $this->initial_tax);
$value .= $id;
if (! $term || is_wp_error($term)) {
return;
}
if (! term_exists($term->slug, $this->target_tax) && (get_term_by(
'slug',
$term->term_id,
$this->target_tax
) == false)) {
if ($term->parent === 0) {
// add top level first to make sure they are created one we assign the children parent logic
$this->clone_term($term, $this->target_tax, true);
} else {
$this->clone_term($term, $this->target_tax);
}
if ($this->cloned_term) {
$this->assign_posts($term, $this->cloned_term);
?>
✔ <a target="_blank"
href="<?php echo admin_url("term.php?taxonomy=" . $this->cloned_term->taxonomy . "&tag_ID={$this->cloned_term->term_id}&post_type=" . $this->post_type . ""); ?>"><?php echo $this->cloned_term->term_id ?></a> ==
<?php }
}
}
echo $value;
}
/**
* @param WP_Term $term
* @param string $target_taxonomy
* @param bool $is_top_level
*/
public function clone_term(WP_Term $term, $target_taxonomy, $is_top_level = false)
{
if (! $term instanceof WP_Term || $term->term_id == 1) {
return;
}
$args = [
'description' => $term->description,
'slug' => $term->slug
];
if (! $is_top_level) {
// Find parent object
$term_parent = get_term_by('id', $term->parent, $term->taxonomy);
if ($term_parent) {
// We find his cloned version for the new taxonomy
$cloned_term_parent = get_term_by('slug', $term_parent->slug, $target_taxonomy);
if ($cloned_term_parent) {
// Assign to term args
$args = array_merge($args, [
'parent' => $cloned_term_parent->term_id
]);
}
}
}
// Create new term
wp_insert_term($term->name, $target_taxonomy, $args);
// New term is available
$cloned_term = get_term_by('slug', $term->slug, $target_taxonomy);
if ($cloned_term) {
// Copy extra data once the term is created
$this->cloned_term = $cloned_term;
$acf_field_icon = 'svg_icon';
$term_icon = get_field($acf_field_icon, $term->taxonomy . '_' . $term->term_id);
$cloned_term_icon = get_field(
$acf_field_icon,
$this->cloned_term->taxonomy . '_' . $this->cloned_term->term_id
);
// Acf field exists in old and is null in new term
if ($term_icon && ! $cloned_term_icon) {
update_field(
$acf_field_icon,
$term_icon,
$this->cloned_term->taxonomy . '_' . $this->cloned_term->term_id
);
}
}
}
/**
* @param WP_Term $term
* @param WP_Term $cloned_term
*/
public function assign_posts(WP_Term $term, WP_Term $cloned_term)
{
// All post using initial term/taxonomy
$post_args = [
'posts_per_page' => -1,
'post_type' => $this->post_type,
'tax_query' => [
[
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term->term_id,
'include_children' => false
]
]
];
$posts = get_posts($post_args);
if ($posts) {
foreach ($posts as $post) {
// Verify that the term exists and has not been attached to current post
if ((bool)term_exists(
$cloned_term->term_id,
$cloned_term->taxonomy,
($cloned_term->parent !== 0 ? $cloned_term->taxonomy : null)
) && ! has_term(
$cloned_term->slug,
$cloned_term->taxonomy,
$post->ID
)) {
// Set cloned term to post
wp_set_object_terms($post->ID, $cloned_term->term_id, $cloned_term->taxonomy, true);
}
}
}
}
}
$migrate_category = new Migrate_Category('category', 'faq_category', 'hrf_faq');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment