Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mralaminahamed/252c8d3bf27aacc76a759f7ad8a290de to your computer and use it in GitHub Desktop.

Select an option

Save mralaminahamed/252c8d3bf27aacc76a759f7ad8a290de to your computer and use it in GitHub Desktop.
Duplicate Portfolio Items from Artist List for Mixing and Production
<?php
/**
* Duplicate arts_portfolio_item post ID 1277 for each artist in the list
* Run this code ONLY ONCE — preferably in a temporary mu-plugin or Code Snippets
* After execution, remove or comment out the code to prevent accidental re-runs.
*/
if (!defined('ABSPATH')) {
exit;
}
// IMPORTANT: Change this to false after testing
$actually_insert = true; // ← Set to false to do a dry run (only prints what would happen)
$template_post_id = 1277;
$post_type = 'arts_portfolio_item';
$artists = [
'JoAnn Rosario',
'Mark Hamill',
'Iamsu!',
'Hi-C',
'El Debarge',
'AGNEZ MO',
'Dj Dahi',
'Dave Jerden',
'Unsung',
'Warm Brew',
'Prince Royal',
'Machine Gun Kelly',
'Andrea Dey',
'Blake Shelton',
'Michael Jackson',
'American Idol',
'Brittany Spears',
'TLC',
'Carla Olson',
'Peter Grant',
'Bishop Lamont',
'Rick Fox',
'Tweed Cadillac',
'NEXT',
'Cicadastone',
'Neal Patrick Harris',
'Baron Davis',
'Doc McKinney',
'Kelly Clarkson',
'Phillip Phillips',
'Tom Petty',
'Honey Rider',
'Sabrina Starke',
'Twiggy',
'Adam Lambert',
'Kristy Almeida',
'Jason Castro',
'Poema',
'All Angels',
'Russell Watson',
'Eric Clapton',
'Kurupt',
'Fred Durst',
'Dj Kid Fresh',
'Nipsey Hussle',
'Judith Hill',
'Songs From Scratch',
'Let Live',
];
// ────────────────────────────────────────────────
// Helper: Convert string to kebab-case slug
// ────────────────────────────────────────────────
function to_kebab_case($string) {
// Replace & → and, remove non-alphanumeric except spaces & hyphens
$string = str_replace('&', 'and', $string);
$string = preg_replace('/[^a-zA-Z0-9\s-]/', '', $string);
$string = trim(preg_replace('/\s+/', ' ', $string));
$string = strtolower($string);
$string = str_replace(' ', '-', $string);
$string = preg_replace('/-+/', '-', $string);
return $string;
}
// ────────────────────────────────────────────────
// Main duplication logic
// ────────────────────────────────────────────────
$template = get_post($template_post_id);
if (!$template || $template->post_type !== $post_type) {
wp_die("Template post ID {$template_post_id} not found or wrong post type.");
}
echo "<pre>\n";
echo "Starting duplication of arts_portfolio_item #{$template_post_id}\n";
echo "───────────────────────────────────────────────\n\n";
foreach ($artists as $index => $name) {
$name = trim($name);
if (empty($name)) continue;
$new_title = $name;
$new_slug = to_kebab_case($name);
// Check if slug already exists to avoid duplicates
$existing = get_posts([
'post_type' => $post_type,
'post_status' => 'any',
'name' => $new_slug,
'posts_per_page' => 1,
'fields' => 'ids',
]);
if (!empty($existing)) {
echo "SKIP: '{$name}' → slug '{$new_slug}' already exists (ID {$existing[0]})\n";
continue;
}
$new_post = [
'post_title' => $new_title,
'post_name' => $new_slug,
'post_content' => $template->post_content,
'post_excerpt' => $template->post_excerpt,
'post_status' => $template->post_status,
'post_type' => $post_type,
'post_author' => $template->post_author,
'menu_order' => $template->menu_order,
'ping_status' => $template->ping_status,
'comment_status' => $template->comment_status,
];
if ($actually_insert) {
$new_id = wp_insert_post($new_post, true);
if (is_wp_error($new_id)) {
echo "ERROR: '{$name}' → {$new_id->get_error_message()}\n";
continue;
}
// Copy all post meta
$meta = get_post_meta($template_post_id);
foreach ($meta as $key => $values) {
foreach ($values as $value) {
// Handle serialized data properly
add_post_meta($new_id, $key, maybe_unserialize($value));
}
}
// Copy featured image
$thumbnail_id = get_post_thumbnail_id($template_post_id);
if ($thumbnail_id) {
set_post_thumbnail($new_id, $thumbnail_id);
}
// Copy terms (taxonomies)
$taxonomies = get_object_taxonomies($post_type);
foreach ($taxonomies as $tax) {
$terms = wp_get_object_terms($template_post_id, $tax, ['fields' => 'ids']);
if (!empty($terms) && !is_wp_error($terms)) {
wp_set_object_terms($new_id, $terms, $tax, false);
}
}
echo "Created: '{$name}' → ID {$new_id} / {$new_slug}\n";
} else {
echo "Would create: '{$name}' → {$new_slug}\n";
}
}
echo "\n───────────────────────────────────────────────\n";
echo "Duplication complete.\n";
echo "</pre>";
// Optional: Uncomment to see PHP errors if something fails
// ini_set('display_errors', 1); error_reporting(E_ALL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment