Let’s say you built a MyListing website that’s been rolling along with a Listing Type named ‘Gyms.’ Now, you’ve decided to rename ‘Gyms’ to ‘Clubs.’
Unfortunately, it’s not as easy as just changing the name of the Listing Type. You have to think about all those Listings published under the old name of ‘Gyms.’
What we are going to do with the steps below is reattach all of the Listings that were previously published under the ‘Gyms’ Listing Type with the new name for that Listing Type…’Clubs.’
Customization Notes
- Add the code snippet to your website and adjust Lines 5-6 of the code snippet, replacing ‘gyms’ with ‘clubs.’
add_action(
"init",
function () {
if (
!isset($_GET["update_listing_type"]) ||
!current_user_can("administrator")
) {
return;
}
$old_type = "gyms";
$new_type = "clubs";
$next_data = 100;
$offset = 0;
do {
$listings = (array) get_posts([
"post_type" => "job_listing",
"offset" => $offset,
"posts_per_page" => $next_data,
"post_status" => "any",
"meta_query" => [
[
"key" => "_case27_listing_type",
"value" => $old_type,
],
],
]);
printf(
"Fetching listings from listing %d to %d
",
$offset + 1,
$offset + $next_data
);
flush();
ob_flush();
foreach ($listings as $listing) {
update_post_meta(
$listing->ID,
"_case27_listing_type",
$new_type
);
$updated = wp_update_post(["ID" => $listing->ID]);
}
$offset = !$offset ? $next_data : $offset + $next_data;
} while (!empty($listings));
exit("All listings are updated, you can close this window.");
},
250
);- Paste the following URL in your browser, replacing ‘domain.com’ with your domain.
https://domain.com/?update_listing_type=1- Hit ‘ENTER’ in your browser’s address bar to execute the code snippet.
- Deactivate (or remove) the code snippet once you are done.
