我正在尝试向我的Wordpress网站添加功能,即每次我添加帖子(自定义帖子类型)时,都会创建另一个帖子(不同的自定义帖子类型)。
我尝试在功能文件中添加操作,但似乎不起作用。
functions.php
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_post', 'add_notification');
我还尝试了其他钩子,例如new_to_publish和publish_post
因此,我希望在添加锻炼帖子时会有一个新帖子(通知)。
我认为我忘记了这很愚蠢,但是好像被困了3天。
答案 0 :(得分:1)
对于自定义帖子类型,我们必须使用类似 add_action('publish_custom-post-name','function'); 您必须使用Like Like
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_exercises', 'add_notification');
请尝试这个。如果有任何疑问,请随时询问。