Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to embed a 'New Post' form for one Custom Post Type inside of a 'New Post' form for another Custom Post Type?

When a user adds a new post for the custom type 'Event', they should also be able to add a new post for the custom type 'Session' without leaving the 'New Event' page. The user should also be able to add tags for the 'Session' post only.

This is almost the same as adding a custom metabox for the 'Event' post type, with the only exception being that the Event's 'Session' data should be saved as a post rather than as a term.

share|improve this question

1 Answer 1

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 and gallery.
  • the meta box appears in portfolio and contains only one textarea field that is used for the content of the auto generated gallery 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
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.