Yes, it is possible to hook into save_post
action of one post type and do a wp_insert_post
in the other post type.
Notes:
- the Meta Box is only generated when a new post is being created.
- the post types involved are
portfolio
andgallery
. - the meta box appears in
portfolio
and contains only one textarea field that is used for the content of the auto generatedgallery
post. - the title of the auto generated post is the same as the original post.
- adjust for the proper post types.
- add fields as required.
- adjust the post array
$add_cpt_clone
as required. - use
$p_id
to add tags (or custom taxonomy) to the auto-generated post. Refer to this Answer to see how to.
add_action( 'add_meta_boxes', 'add_custom_box_wpse_76945' );
add_action( 'save_post', 'save_postdata_wpse_76945', 10, 2 );
/**
* Meta box for PORTFOLIO post type
*/
function add_custom_box_wpse_76945()
{
// Show ONLY when creating a NEW POST
global $pagenow;
if( 'post-new.php' != $pagenow )
return;
// Add meta box to PORTFOLIO
add_meta_box(
'sectionid_wpse_76945',
__( 'Create Post in another CPT' ),
'inner_custom_box_wpse_76945',
'portfolio'
);
}
/**
* Meta box content
*/
function inner_custom_box_wpse_76945( $post )
{
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_wpse_76945' );
// Field for data entry
echo '<h4>THE_CONTENT</h4>';
echo '<textarea id="the_content_wpse_76945" name="the_content_wpse_76945" cols="90" rows="5"></textarea>';
}
/**
* Creates a new post in GALLERY post type
*/
function save_postdata_wpse_76945( $post_id, $post_object )
{
// Auto save?
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Correct post_type
if ( 'portfolio' != $post_object->post_type )
return;
// Security
if (
!isset($_POST['noncename_wpse_76945'])
|| !wp_verify_nonce( $_POST['noncename_wpse_76945'], plugin_basename( __FILE__ ) )
)
return;
// Prepare contents
$add_cpt_clone = array(
'post_title' => $post_object->post_title,
'post_content' => $_POST['the_content_wpse_76945'],
'post_status' => 'publish',
'post_type' => 'gallery'
);
// Insert the post into the database
$p_id = wp_insert_post( $add_cpt_clone );
// Use $p_id to insert new terms
}