Skip to content

Commit

Permalink
replaces direct sql query with a wordpress function
Browse files Browse the repository at this point in the history
  • Loading branch information
kasprzyk-sz committed Nov 8, 2024
1 parent 782503d commit 13aa3d7
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions wp-content/plugins/embl-taxonomy/includes/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,34 +461,28 @@ public function sync_taxonomy(WP_REST_Request $request = null) {

// Find a replacement term if the term is used in posts and is deprecated
if ($is_deprecated && $wp_term->count > 0) {
$replacement_term_id = null;
foreach ($wp_taxonomy as $potential_replacement) {
if ($potential_replacement->term_id !== $wp_term->term_id &&
array_key_exists(EMBL_Taxonomy::META_NAME, $potential_replacement->meta) &&
$potential_replacement->meta[EMBL_Taxonomy::META_NAME] === $wp_term->meta[EMBL_Taxonomy::META_NAME] &&
!array_key_exists(EMBL_Taxonomy::META_DEPRECATED, $potential_replacement->meta)) { // Exclude deprecated terms
$replacement_term_id = $potential_replacement->term_id;
break;
}
}
$replacement_term_id = null;
foreach ($wp_taxonomy as $potential_replacement) {
if ($potential_replacement->term_id !== $wp_term->term_id &&
array_key_exists(EMBL_Taxonomy::META_NAME, $potential_replacement->meta) &&
$potential_replacement->meta[EMBL_Taxonomy::META_NAME] === $wp_term->meta[EMBL_Taxonomy::META_NAME] &&
!array_key_exists(EMBL_Taxonomy::META_DEPRECATED, $potential_replacement->meta)) {
$replacement_term_id = $potential_replacement->term_id;
break;
}
}

// If a replacement term is found, proceed with updating posts only
if ($replacement_term_id) {
// Get all post IDs associated with the deprecated term
$post_ids = $wpdb->get_col($wpdb->prepare(
"SELECT object_id FROM {$wpdb->term_relationships}
WHERE term_taxonomy_id = %d",
$wp_term->term_id
));
if ($replacement_term_id) {
$post_ids = get_objects_in_term($wp_term->term_id, EMBL_Taxonomy::TAXONOMY_NAME);

foreach ($post_ids as $post_id) {
wp_add_object_terms($post_id, $replacement_term_id, EMBL_Taxonomy::TAXONOMY_NAME);
// Optionally remove the deprecated term from the post
// wp_remove_object_terms($post_id, $wp_term->term_id, EMBL_Taxonomy::TAXONOMY_NAME);
}
}
}

// Replace the deprecated term for each post
foreach ($post_ids as $post_id) {
wp_add_object_terms($post_id, $replacement_term_id, EMBL_Taxonomy::TAXONOMY_NAME);
// Optionally remove the deprecated term from the post
// wp_remove_object_terms($post_id, $wp_term->term_id, EMBL_Taxonomy::TAXONOMY_NAME);
}
}
}

// Replace terms individually in ACF fields if selected and a non-deprecated equivalent exists
if ($acf_selected) {
Expand Down

0 comments on commit 13aa3d7

Please sign in to comment.