harumemo

メモ書きです。

カスタム投稿の複製機能について

add_action('add_meta_boxes', 'add_my_box');
function add_my_box()
{
global $post;

if(get_post_type() === 'booking') {
if ( in_array( $post->post_status, array('publish', 'future', 'private') ) && 0 != $post->ID ) {
add_meta_box('booking', '予約データ複製', 'bkg_post_submitbox_start', 'booking', 'normal', 'high');
}
}
}

//add_action( 'post_submitbox_start', 'bkg_post_submitbox_start' );
function bkg_post_submitbox_start() {
global $post;

if(get_post_type() === 'booking') {
if ( in_array( $post->post_status, array('publish', 'future', 'private') ) && 0 != $post->ID ) {
echo '<div id="branch-action" style="margin-bottom:5px;">';
echo '<p>この予約をコピーする</p>';
echo '<input type="submit" class="button-primary" name="wp_post_branches" value="コピー" />';
echo '</div>';
}
}
}

add_filter( 'pre_post_update', 'bkg_pre_post_update' );
function bkg_pre_post_update( $id ) {

if ( isset( $_POST['wp_post_branches'] ) ) {
// post
$pub = get_post( $id, ARRAY_A );
unset( $pub['ID'] );
$pub['post_status'] = 'draft';
$pub = apply_filters( 'wpbs_pre_publish_to_draft_post', $pub );
$draft_id = wp_insert_post( $pub );

//custom field suite
global $cfs;
$options = array('format' => 'raw');
$field_data = $cfs->get(false, $id, $options);
$post_data = array('ID' => $draft_id);
$cfs->save($field_data, $post_data);

// postmeta
/*
$keys = get_post_custom_keys( $id );
$custom_field = array();
foreach ( (array) $keys as $key ) {
if ( preg_match( '/^_feedback_/', $key ) )
continue;

if ( preg_match( '/_wp_old_slug/', $key ) )
continue;

$key = apply_filters( 'wpbs_publish_to_draft_postmeta_filter', $key );

$values = get_post_custom_values($key, $id );
foreach ( $values as $value ) {
add_post_meta( $draft_id, $key, $value );
}
}
*/

//attachment
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $id );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $attachment ) {
$new = array(
'post_author' => $attachment->post_author,
'post_date' => $attachment->post_date,
'post_date_gmt' => $attachment->post_date_gmt,
'post_content' => $attachment->post_content,
'post_title' => $attachment->post_title,
'post_excerpt' => $attachment->post_excerpt,
'post_status' => $attachment->post_status,
'comment_status' => $attachment->comment_status,
'ping_status' => $attachment->ping_status,
'post_password' => $attachment->post_password,
'post_name' => $attachment->post_name,
'to_ping' => $attachment->to_ping,
'pinged' => $attachment->pinged,
'post_modified' => $attachment->post_modified,
'post_modified_gmt' => $attachment->post_modified_gmt,
'post_content_filtered' => $attachment->post_content_filtered,
'post_parent' => $draft_id,
'guid' => $attachment->guid,
'menu_order' => $attachment->menu_order,
'post_type' => $attachment->post_type,
'post_mime_type' => $attachment->post_mime_type,
'comment_count' => $attachment->comment_count
);
$new = apply_filters( 'wpbs_pre_publish_to_draft_attachment', $new );
$attachment_newid = wp_insert_post( $new );
$keys = get_post_custom_keys( $attachment->ID );

$custom_field = array();
foreach ( (array) $keys as $key ) {
$value = get_post_meta( $attachment->ID, $key, true );

add_post_meta( $attachment_newid, $key, $value );
}
}
}

//tax
$taxonomies = get_object_taxonomies( $pub['post_type'] );
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($id, $taxonomy, array( 'orderby' => 'term_order' ));
$post_terms = apply_filters( 'wpbs_pre_publish_to_draft_taxonomies', $post_terms );
$terms = array();
for ($i=0; $i<count($post_terms); $i++) {
$terms[] = $post_terms[$i]->slug;
}
wp_set_object_terms($draft_id, $terms, $taxonomy);
}

add_post_meta($draft_id, '_wpbs_pre_post_id', $id);

if ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) {
wp_safe_redirect( admin_url( 'post.php?post=' . $draft_id . '&action=edit' ) );
exit;
}
}
}

 

プラグインを使わずにカスタム投稿の記事をコピー(複製)する wordpress | WordPress & Web Application Note

kachibito.net