如何通过代码复制 Elementor 页面?

How to duplicate Elementor page via code?

提问人:Ed Dias 提问时间:11/17/2023 最后编辑:Ed Dias 更新时间:11/17/2023 访问量:28

问:

我创建了一个克隆特定 Wordpress 页面的插件,这个我称之为原始页面的页面是用 Elementor 创建的。

激活插件时,页面被完美复制,但是当我访问这个克隆页面时,它在布局上与原始页面并不完全相同,但是当我在 Elementor 版本中打开克隆页面时,页面是一样的,保留了整个结构。

我检查了“_postmeta”表,meta_keys与原始页面相同,我已经比较了值,它们在meta_value字段中是相同的。

秘诀是什么?为什么当我访问页面时,它看起来不一样?布局看起来不同。

function duplicate_page_111_as_draft( $new_status, $old_status, $post ) {
    global $wpdb;

    if ( 'sda_churches' === $post->post_type && $new_status === 'publish' && $old_status !== 'publish' ) {
        $original_post_id = 111; // ID of the page you want to duplicate
    
        // Get the original post data
        $post = get_post($original_post_id);
    
        // Set the new post author (optional, you can customize as needed)
        $new_post_author = $post->post_author;
    
        // Create an array with new post data
        $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 . '-copy', // You may want to adjust this to avoid conflicts
            'post_parent'    => $post->post_parent,
            'post_password'  => $post->post_password,
            'post_status'    => 'publish',
            'post_title'     => $post->post_title . ' Copy', // You may want to adjust this to avoid conflicts
            'post_type'      => $post->post_type,
            'to_ping'        => $post->to_ping,
            'menu_order'     => $post->menu_order
        );
    
        // Insert the new post
        $new_post_id = wp_insert_post($args);
    
        // Duplicate all post terms and set them to the new post draft
        $taxonomies = get_object_taxonomies($post->post_type);
        foreach ($taxonomies as $taxonomy) {
            $post_terms = wp_get_object_terms($original_post_id, $taxonomy, array('fields' => 'slugs'));
            wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
        }
    
        // Duplicate all post meta in two SQL queries
        $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$original_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);
        }
    }
}
add_action( 'transition_post_status', 'duplicate_page_111_as_draft', 10, 3 );

我还检查了一些东西,当我编辑克隆的页面时,进行更改并保存和访问该页面,然后它看起来与原始页面相同。

但是我想知道为什么页面在克隆过程中不保持不变,我不想必须进入页面,更改它并保存它才能使其工作。因为在站点运行期间将进行多次克隆。

PHP wordpress 插件

评论


答: 暂无答案