Astra_Addon_Theme_Builder::create_layout_duplicate( int $post_id )

Create a duplicate of a layout post with all its metadata


Description


Parameters

$post_id

(int) (Required) The original post ID to duplicate.


Return

(int|WP_Error) The new post ID on success, WP_Error on failure.


Source

File: addons/advanced-hooks/classes/class-astra-addon-theme-builder.php

		public function create_layout_duplicate( $post_id ) {
			$original_post = get_post( $post_id );

			if ( ! $original_post ) {
				return new WP_Error( 'post_not_found', 'Post not found' );
			}

			$new_post = array(
				'post_type'    => $original_post->post_type,
				'post_title'   => $original_post->post_title . ' - ' . __( 'Copy', 'astra-addon' ),
				'post_content' => $original_post->post_content,
				'post_status'  => 'draft',
				'post_author'  => get_current_user_id(),
			);

			$new_post_id = wp_insert_post( $new_post );

			if ( is_wp_error( $new_post_id ) ) {
				return new WP_Error( 'post_creation_failed', 'Failed to create duplicate post' );
			}

			$meta_keys = array(
				'ast-advanced-hook-layout',
				'ast-advanced-hook-template-type',
				'ast-advanced-hook-enabled',
				'ast-advanced-hook-location',
				'ast-advanced-hook-content',
			);

			foreach ( $meta_keys as $meta_key ) {
				$meta_value = get_post_meta( $post_id, $meta_key, true );
				if ( $meta_value ) {
					update_post_meta( $new_post_id, $meta_key, $meta_value );
				}
			}

			return $new_post_id;
		}

Changelog

Changelog
Version Description
4.11.13 Introduced.


User Contributed Notes

You must log in before being able to contribute a note or feedback.