Skip to content

Instantly share code, notes, and snippets.

@silvaitamar
Last active May 22, 2024 07:54
Show Gist options
  • Save silvaitamar/b8d991edfc8d0277899be66a25939473 to your computer and use it in GitHub Desktop.
Save silvaitamar/b8d991edfc8d0277899be66a25939473 to your computer and use it in GitHub Desktop.
Função para copiar as categorias associadas aos posts de um tipo customizado para uma nova taxonomia customizada
<?php
/**
* Function to copy categories associated with custom post type to the new custom taxonomy "taxonomy-slug".
*/
function copy_custom_post_type_categories() {
// Set arguments to retrieve custom post type posts.
$args = array(
'post_type' => 'post-type-slug', // Replace 'post-type-slug' with your custom post type name.
'posts_per_page' => -1,
);
// Retrieve custom post type posts.
$custom_posts = new WP_Query($args);
// Check if there are custom post type posts.
if ($custom_posts->have_posts()) {
// Loop through custom post type posts.
while ($custom_posts->have_posts()) {
$custom_posts->the_post();
// Get all categories associated with the post.
$post_categories = get_the_category();
// Check if the post has associated categories.
if ($post_categories) {
// Loop through the categories associated with the post.
foreach ($post_categories as $category) {
// Check if the category already exists in the new taxonomy.
$existing_term = term_exists($category->name, 'taxonomy-slug');
// If the term does not exist in the new taxonomy, create it.
if ($existing_term === 0 || $existing_term === null) {
$args = array(
'description' => $category->description,
'slug' => $category->slug,
);
$existing_term = wp_insert_term($category->name, 'taxonomy-slug', $args);
// Check if there was an error inserting the term.
if (is_wp_error($existing_term)) {
error_log('Error inserting term ' . $category->name . ' in the new taxonomy: ' . $existing_term->get_error_message());
continue; // Skip to the next iteration of the loop in case of an error.
}
}
// Check if the category has a parent in the default taxonomy.
if ($category->parent != 0) {
// Get the corresponding parent term in the new taxonomy.
$parent_term_exists = term_exists(get_cat_name($category->parent), 'taxonomy-slug');
// If the parent term does not exist, create it.
if ($parent_term_exists === 0 || $parent_term_exists === null) {
$parent_args = array(
'description' => get_category($category->parent)->description,
'slug' => get_category($category->parent)->slug,
);
$parent_term_exists = wp_insert_term(get_cat_name($category->parent), 'taxonomy-slug', $parent_args);
// Check if there was an error inserting the parent term.
if (is_wp_error($parent_term_exists)) {
error_log('Error inserting parent term ' . get_cat_name($category->parent) . ' in the new taxonomy: ' . $parent_term_exists->get_error_message());
continue; // Skip to the next iteration of the loop in case of an error.
}
}
// Assign the parent term to the category term in the new taxonomy.
wp_update_term($existing_term['term_id'], 'taxonomy-slug', array('parent' => $parent_term_exists['term_id']));
}
// Assign the new taxonomy term to the post.
wp_set_post_terms(get_the_ID(), $existing_term['term_id'], 'taxonomy-slug', true);
}
}
}
wp_reset_postdata(); // Restore post data.
}
}
add_action('init', 'copy_custom_post_type_categories', 999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment