As discussed here, I’ve been trying to programatically change pages slugs, to clean them from unnecessary trailing id numbers.
But after doing so, instead of pages with the new slugs, I have duplicated pages, one with the old slug and one with the new slug. I must have done something stupid, see:
This is my code:
foreach(page('exhibitions')->children() as $e) {
$kirby->impersonate('kirby');
// Clean the slug of trailing ids
$clean = preg_split('{(-[0-9]*)$}', $e->slug())[0];
// If there are more than one exhibition with the same clean slug, just leave them alone, we'll deal manually with those
if (page('exhibitions')->children()->search($clean, 'id') && page('exhibitions')->children()->search($clean, 'id')->count() > 1) {
dump(page('exhibitions')->children()->search($clean, 'id'));
// ...if actual slug and clean slug are not equal, then proceed with the slug change
} elseif ($e->slug() != $clean) {
try {
$e->changeSlug($clean);
echo 'done' . "<br />";
} catch (Exception $e) {
dump($e);
}
} else {
echo 'nothing to change' . '<br />';
};
What am I missing ?
/edit
Funnily enough, I just noticed all old slug pages share the same sorting number: 1!
Now that is puzzling to me.
Thanks