Let’s say you have several listings assigned to a particular listing type and for whatever reason you want to assign them to a different listing type. To do so, you would need to edit each listing.
The Solution
What we are going to do with the steps below is reassign all of the listings that are currently published under the “Gyms” listing type with the new “Clubs” listing type.
- Add the code snippet below to your website.
PHP
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
);- Adjust lines 10-11 of the code snippet, replacing “gyms” with “clubs.”
- 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.
