Skip to content

Instantly share code, notes, and snippets.

@hmbashar
Last active October 22, 2024 06:20
Show Gist options
  • Save hmbashar/e9d5081f75f493a0b3c9e17c1565e191 to your computer and use it in GitHub Desktop.
Save hmbashar/e9d5081f75f493a0b3c9e17c1565e191 to your computer and use it in GitHub Desktop.
To copy all posts from the custom post type abcbizrev_reviews to another custom post type revix_reviews, you can use a simple custom script or run SQL queries to duplicate the posts in the WordPress database. Here's a safe way to do it programmatically via PHP:
<?php
/**
* You can place this script in your theme’s functions.php file or in a custom plugin to execute the process. After running, you may want to remove it to avoid duplicating the posts again accidentally.
*/
function copy_abcbizrev_reviews_to_revix_reviews() {
// Get all posts of the custom post type 'abcbizrev_reviews'
$args = array(
'post_type' => 'abcbizrev_reviews',
'posts_per_page' => -1, // Get all posts
'post_status' => 'any', // Include all post statuses
);
$posts = get_posts($args);
if ($posts) {
foreach ($posts as $post) {
// Set up the new post data
$new_post = array(
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'post_status' => $post->post_status,
'post_type' => 'revix_reviews', // New post type
'post_author' => $post->post_author,
'post_date' => $post->post_date,
'post_date_gmt' => $post->post_date_gmt,
);
// Insert the new post into the new post type
$new_post_id = wp_insert_post($new_post);
// If you want to copy metadata, too
if (!is_wp_error($new_post_id)) {
$meta_data = get_post_meta($post->ID);
foreach ($meta_data as $meta_key => $meta_values) {
foreach ($meta_values as $meta_value) {
update_post_meta($new_post_id, $meta_key, $meta_value);
}
}
}
}
}
}
// Hook the function to run on init, but you should run this only once and remove the hook afterward
add_action('init', 'copy_abcbizrev_reviews_to_revix_reviews');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment