I am trying to:
I ran one code in toolset-> custom code. The snippet is on demand and inactive via run now button the code ran fine but after that when I try to open toolset-> settings it showing error. other pages are loading fine.
what I tired:
I have rename the snippet in file manager then the toolset-> setting is loading.
I have run the same code in run always option several times before but it doesn´t cause any issue . I want this code to run only once by manual trigger so I tried on demand option and clicked run away.
`$current_user = wp_get_current_user();
if ($current_user->ID === xxxx) {
error_log('✅ Sync triggered by authorized user ID.');
run_xxxx_location_sync();
} else {
error_log('⛔ Unauthorized sync attempt by user ID: ' . $current_user->ID);
}
function run_xxxx_location_sync() {
error_log('✅ Sync code started running on demand.');
if (!function_exists('toolset_get_related_posts')) {
error_log('❌ Toolset function toolset_get_related_posts not available.');
return;
}
$relationship = 'xxxx-xxxx';
$post_type = 'xxxx';
$taxonomy_slug = 'xxxx';
$batch_size = 100;
$paged = 1;
$processed = 0;
$errors = 0;
do {
$posts = get_posts([
'post_type' => $post_type,
'posts_per_page' => $batch_size,
'paged' => $paged,
'post_status' => 'publish',
'fields' => 'ids',
]);
if (empty($posts)) {
break;
}
foreach ($posts as $post_id) {
$processed++;
$related_parents = toolset_get_related_posts(
$post_id,
$relationship,
'child',
100,
0
);
$terms_to_set = [];
if (!empty($related_parents)) {
foreach ($related_parents as $parent_id) {
$terms = wp_get_post_terms($parent_id, $taxonomy_slug, ['fields' => 'ids']);
if (!empty($terms)) {
$terms_to_set = array_merge($terms_to_set, $terms);
}
}
}
$terms_to_set = array_unique($terms_to_set);
if (!empty($terms_to_set)) {
wp_set_post_terms($post_id, $terms_to_set, $taxonomy_slug);
} else {
$errors++;
}
}
error_log("⏳ Batch {$paged} processed. Total processed: {$processed}");
$paged++;
} while (count($posts) === $batch_size);
error_log("✅ All {$processed} posts processed.");
if ($errors) {
error_log("⚠️ {$errors} posts had no terms to set.");
} else {
error_log("🎉 All posts updated successfully with taxonomy terms.");
}
}`
Please guide me how to fix this issue.