Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mralaminahamed/53bc9f60ed8bf8f56e56a562a7b175bd to your computer and use it in GitHub Desktop.

Select an option

Save mralaminahamed/53bc9f60ed8bf8f56e56a562a7b175bd to your computer and use it in GitHub Desktop.
Duplicate Portfolio Items from Artist List for Film and Television
<?php
/**
* Duplicate arts_portfolio_item post ID 433 for each title in the list
*
* Run this code ONLY ONCE in a WordPress environment (e.g., via Code Snippets plugin,
* temporary mu-plugin, or custom admin page).
* After successful execution, deactivate or remove the code.
*/
if (!defined('ABSPATH')) {
exit;
}
// Set to false for a dry run (preview only); change to true to actually create posts
$actually_insert = true;
$template_post_id = 433;
$post_type = 'arts_portfolio_item';
$items = [
'Coyote Ugly',
'Body of Proof',
'The Nameless',
'Barbershop',
'Soul Food',
'Love 15',
'Off Season',
'MGM Hotels',
'Streetcar',
'Blood Brothers',
'Waiting to Exhale',
'Soccer Mom',
'Diabolical',
'Gore Vidal',
'A Play on Words',
'Feels So Good',
'Fetching',
'American Idol',
'Gleep TV',
'Phenomenon',
'Hangover 2 - Unauthorized',
'Back in the Game',
'Global Beauty Masters',
'How to Marry a Billionaire',
'Hot Spots',
'The Nameless',
'Bullet Proof',
'Supermoms',
];
// Helper: Convert string to safe kebab-case slug
function to_kebab_case($string) {
$string = trim($string);
$string = str_replace(['&', '+'], ['and', 'plus'], $string);
$string = preg_replace('/[^a-zA-Z0-9\s-]/', '', $string); // Remove most special chars
$string = preg_replace('/\s+/', ' ', $string);
$string = strtolower($string);
$string = str_replace(' ', '-', $string);
$string = preg_replace('/-+/', '-', $string);
$string = trim($string, '-');
return $string;
}
// ────────────────────────────────────────────────
// Execution
// ────────────────────────────────────────────────
$template = get_post($template_post_id);
if (!$template || $template->post_type !== $post_type) {
wp_die("Template post ID {$template_post_id} not found or is not of type '{$post_type}'.");
}
echo "<pre>\n";
echo "Duplication process for arts_portfolio_item #{$template_post_id}\n";
echo "───────────────────────────────────────────────\n\n";
foreach ($items as $item) {
$title = trim($item);
if (empty($title)) continue;
$slug = to_kebab_case($title);
// Prevent duplicate creation based on slug
$existing = get_posts([
'post_type' => $post_type,
'post_status' => 'any',
'name' => $slug,
'posts_per_page' => 1,
'fields' => 'ids',
]);
if (!empty($existing)) {
echo "SKIP: \"{$title}\" (slug: {$slug}) already exists (ID: {$existing[0]})\n";
continue;
}
$new_post_data = [
'post_title' => $title,
'post_name' => $slug,
'post_content' => $template->post_content,
'post_excerpt' => $template->post_excerpt,
'post_status' => $template->post_status, // inherit draft/publish/etc.
'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_post_id = wp_insert_post($new_post_data, true);
if (is_wp_error($new_post_id)) {
echo "ERROR: \"{$title}\"{$new_post_id->get_error_message()}\n";
continue;
}
// Copy all post meta
$meta_keys = get_post_meta($template_post_id);
foreach ($meta_keys as $meta_key => $meta_values) {
foreach ($meta_values as $meta_value) {
$unserialized = maybe_unserialize($meta_value);
add_post_meta($new_post_id, $meta_key, $unserialized);
}
}
// Copy featured image (thumbnail)
$thumbnail_id = get_post_thumbnail_id($template_post_id);
if ($thumbnail_id) {
set_post_thumbnail($new_post_id, $thumbnail_id);
}
// Copy taxonomies/terms
$taxonomies = get_object_taxonomies($post_type);
foreach ($taxonomies as $taxonomy) {
$terms = wp_get_object_terms($template_post_id, $taxonomy, ['fields' => 'ids']);
if (!empty($terms) && !is_wp_error($terms)) {
wp_set_object_terms($new_post_id, $terms, $taxonomy, false);
}
}
echo "CREATED: \"{$title}\" → ID {$new_post_id} / slug: {$slug}\n";
} else {
echo "Would create: \"{$title}\" → slug: {$slug}\n";
}
}
echo "\n───────────────────────────────────────────────\n";
echo "Process completed.\n";
echo "</pre>";
// Optional debugging
// error_reporting(E_ALL); ini_set('display_errors', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment