2017-11-28 12 views
1

私は別のCPTに1つのCPTを動的に追加します。wp_insert_post()を使用 - 複製を作成

function cpt_to_cpt(){  
// Grab posts 
    $args = array(
     'post_type'  => ' custom_type1 ', 
     'order'   => 'ASC', 
     'post_status' => 'publish', 
     'numberposts' => -1, 
     ); 

    $posts = get_posts($args); 

    foreach ($posts as $post) { 
     wp_insert_post(array(
      'post_type'  => 'custom_type2', 
      'post_title' => $post->post_title, 
      'post_date'  => $post->post_date, 
      'post_author' => $post->post->author, 
      'post_status' => 'publish', 
      ) 
     ); 
     } 
add_action('init', 'cpt_to_cpt'); 

答えて

0

使用してみてください:

wp_loaded

add_action('wp_loaded', 'cpt_to_cpt'); 

または

wp

wp_insert_post()を使用している場合、それは私がすべてのアイデアを add_action('init', 'function_name');際にどのようなフック、単にそれらを追加するために使用するために複製を作成し、
add_action('wp', 'cpt_to_cpt') 
+1

'add_action( 'wp'、 'cpt_to_cpt')'ありがとうございます。条件を使ってコードを修正しました: 'if(!get_page_by_title($ post-> post_title、OBJECT、 'custom_type2'))' –

関連する問題