How to Duplicate WordPress Post or Page?

How to Duplicate WordPress Post or Page?

Duplicating a page or a post makes it effortless to build a website. It is much more than merely copying and pasting the content.  

WordPress post and page duplicating can help you adjust theme settings, SEO data, and images with just a click. 

There are multiple instances when you will need to build pages or posts, except the content, that are exactly similar. For example, create sales pages with the same look and feel with just a few content changes. 

Imagine how fun it would be to set the setting once and reuse it again for other pages?

That would be awesome, right?

In the article below, you’ll find ways to duplicate posts or pages in WordPress. You’ll get the idea on how to do it manually, using a plugin, and without a plugin.  

Why Duplicate WordPress Post or Page?

While building a website, sometimes, two or more pages in your site can become identical in its settings. 

Though you can copy and paste the content, you need to adjust the pages’ settings manually. In such a case, duplicating a post or a page is a more natural and efficient option.

By doing so, you can start editing the post or page and start working on it immediately. Plus, it is easier to handle websites with multiple pages.

Now the question is, how can you duplicate a WordPress post or page?

How to Duplicate a WordPress Post or Page?

There are three ways to duplicate a WordPress post or page. Since it’s WordPress, there are plugins to do the job for you. If you do not want to use a plugin and want to do it manually, you can opt for that as well. 

Using Plugins

Plugins ensure safety and ease while you duplicate pages and posts from your website. They integrate easily with the WordPress theme to provide you extended functionalities. The best thing is, you do not need to write a single line of code with these plugins.

Yoast Duplicate Post

For most of the WordPress users, Duplicate Post may be a well-known name. That’s because it is the most popular and go-to option for duplicating pages and posts. 

You can clone everything, from content to even comments associated with a page or a post. To use this plugin, follow the following steps. 

  • Install and activate the plugin.
  • Via Dashboard, go to Posts > All or Page > All.
  • Navigate to the original post/ page you want to copy and click on Clone.
  • You can clone multiple pages at once using Bulk Actions.
Duplicate post in WordPress
Post Duplicate in WordPress

Duplicate Page

Duplicate Page is another excellent plugin with several added features, compared to traditional plugins. You can create duplicates of pages, posts, and custom post types. Once you’ve created a copy, you can save it in any desired form as a draft, public, private, or pending. 

Follow the steps below to use the plugin:

  • Install and activate the plugin.
  • Adjust configuration settings to meet your needs.
  • Go to Pages > All or Posts > All and choose the content you want to duplicate.
  • Click on Duplicate This, and you’re done.
Page Duplicate in WordPress

Duplicate Page and Post

This plugin is the most lightweight and fast. Though it may not have advanced features like other plugins, it is one of the most efficient plugins. 

To duplicate your posts and page using this plugin, follow the following steps:

  • Install and activate the plugin.
  • Go to Posts/ All or Pages/ All.
  • Open up the page or posts you want to clone.
  • Hit the Duplicate button.
Duplicate post and page WordPress
Duplicate post and Page

Without Using Plugins

Yes, plugins are great. But that’s not the only way to duplicate posts and pages. You can choose to do it yourself manually without plugins. Given below are the few ways you can do that. 

Via functions.php code

You can duplicate your posts and pages using the functions.php file. Write a few lines of code to the existing functions.php. But, make sure you back up your site thoroughly before making changes to the functions.php file.

You will need some basic understanding of codes to understand how it works. For now, copy and paste the code below:

/*
 * Function for post duplication. Dups appear as drafts. User is redirected to the edit screen
 */
function rd_duplicate_post_as_draft(){
  global $wpdb;
  if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
    wp_die('No post to duplicate has been supplied!');
  }
  /*
   * Nonce verification
   */
  if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
    return;
  /*
   * get the original post id
   */
  $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
  /*
   * and all the original post data then
   */
  $post = get_post( $post_id );
  /*
   * if you don't want current user to be the new post author,
   * then change next couple of lines to this: $new_post_author = $post->post_author;
   */
  $current_user = wp_get_current_user();
  $new_post_author = $current_user->ID;
  /*
   * if post data exists, create the post duplicate
   */
  if (isset( $post ) && $post != null) {
    /*
     * new post data array
     */
    $args = array(
      'comment_status' => $post->comment_status,
      'ping_status'    => $post->ping_status,
      'post_author'    => $new_post_author,
      'post_content'   => $post->post_content,
      'post_excerpt'   => $post->post_excerpt,
      'post_name'      => $post->post_name,
      'post_parent'    => $post->post_parent,
      'post_password'  => $post->post_password,
      'post_status'    => 'draft',
      'post_title'     => $post->post_title,
      'post_type'      => $post->post_type,
      'to_ping'        => $post->to_ping,
      'menu_order'     => $post->menu_order
    );
    /*
     * insert the post by wp_insert_post() function
     */
    $new_post_id = wp_insert_post( $args );
    /*
     * get all current post terms ad set them to the new post draft
     */
    $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
    foreach ($taxonomies as $taxonomy) {
      $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
      wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
    }
    /*
     * duplicate all post meta just in two SQL queries
     */
    $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
    if (count($post_meta_infos)!=0) {
      $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
      foreach ($post_meta_infos as $meta_info) {
        $meta_key = $meta_info->meta_key;
        if( $meta_key == '_wp_old_slug' ) continue;
        $meta_value = addslashes($meta_info->meta_value);
        $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
      }
      $sql_query.= implode(" UNION ALL ", $sql_query_sel);
      $wpdb->query($sql_query);
    }
    /*
     * finally, redirect to the edit post screen for the new draft
     */
    wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
    exit;
  } else {
    wp_die('Post creation failed, could not find original post: ' . $post_id);
  }
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
/*
 * Add the duplicate link to action list for post_row_actions
 */
function rd_duplicate_post_link( $actions, $post ) {
  if (current_user_can('edit_posts')) {
    $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
  }
  return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

If you want to clone a page instead of a post, just make a minor change by adding a page instead of a post in the last line of the code. 

add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);

Manually Copy and Paste Code

This process might be a tiring one for some as it requires you to copy and paste the code from each page or post that you want to clone. 

Follow the steps below to clone a post or page manually:

  • Open the page or posts you want to clone.
  • Go to More Tools and Options menu.
  • Choose a Code Editor.
  • Copy the code for the page or post you want to clone.
  • Click on New Post or New Page.
  • Open the Code Editor in New Page. 
  • Paste the code.
  • Go to More Tools and Options menu.
  • Choose Visual Editor.
  • The page or post that appears is a clone of an old one.

Conclusion

There are many options on how to duplicate a WordPress post or page. You can choose to use a plugin or clone your page and posts without it too. Plugins make your job easier. Meanwhile, doing it without a plugin lets you take total control of things. 

So, use a method that suits your needs the best. Also, check How to Find WordPress Page ID and post ID, and please let us know if our article was helpful for you.

Leave a Reply

Your email address will not be published.