【发布时间】:2017-11-28 11:22:39
【问题描述】:
我正在动态地将一个 CPT 添加到另一个 CPT。当使用wp_insert_post() 时,它会在我add_action('init', 'function_name'); 时创建重复项,知道使用什么钩子来简单地添加它们:
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');
【问题讨论】:
-
如果你是为一个项目做这个,你可以使用插件吗? wordpress.org/plugins/post-type-switcher
标签: php wordpress custom-post-type