Skip to Content
Menu

duplicate_post_as_draft()

Copy a post and set it as a draft.

PHP April 27, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Admin/Admin.php on line 1550.

    No Hooks

    This function does not have any filters or actions available. Request one?
    PHP
            public function duplicate_post_as_draft(){
                global $wpdb;
    
                if ( !(isset($this->super->get['post']) || isset($this->super->post['post']) || (isset($this->super->request['action']) && $this->super->request['action'] === 'duplicate_post_as_draft')) ){
                    wp_die('No post to duplicate has been supplied!');
                }
    
                $post_id = ( isset($this->super->get['post'] )? intval($this->super->get['post']) : intval($this->super->post['post'])); //Get the original post id
                $post = get_post($post_id); //Get all the original post data
    
                $current_user = wp_get_current_user();
                $new_post_author = $current_user->ID; //Set post author (default by current user). For original author change to: $new_post_author = $post->post_author;
    
                //If post data exists, create the post duplicate
                if ( isset($post) && $post != null ){
    
                    //Insert the post by wp_insert_post() function
                    $new_post_id = wp_insert_post(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 . ' copy',
                        'post_type' => $post->post_type,
                        'to_ping' => $post->to_ping,
                        'menu_order' => $post->menu_order
                    ));
    
                    //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
                    $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"); //DB Query
                    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;
                            $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); //DB Query
                    }
    
                    wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id)); //Redirect to the edit post screen for the new draft
                    exit;
                } else {
                    wp_die('Post creation failed, could not find original post: ' . $post_id);
                }
            }
    

    Override

    This function can not be short-circuited with an override filter. Request one?