php - 每当创建另一个自定义帖子类型时创建自定义帖子类型
问题描述
我正在尝试向我的 Wordpress 网站添加一项功能,即每次添加帖子(自定义帖子类型)时,都会创建另一个帖子(不同的自定义帖子类型)。
我尝试在我的函数文件中添加一个动作,但它似乎不起作用。
函数.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 天了。
解决方案
对于自定义帖子类型,我们必须使用像 add_action('publish_custom-post-name','function'); 这样的钩子。在您的情况下,您必须使用如下所示
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');
请试试这个。如果有任何疑问,请随时询问。
推荐阅读
- soap - Python 的 zeep 模块如何与 API 管理器协同工作?
- javascript - 如何将转换添加到 unicode 字母
- php - 将带有 GMT 的字符串转换为日期时间格式 - php
- docker - dockerfile 无法删除设备或资源繁忙
- math - 3D 插值,哪一个是插值的最佳解决方案?
- bpf - eBPF 的 perf_submit() 也可以在 socket_filter 程序中使用吗?
- google-cloud-platform - Google Cloud Build - 自托管的 bitbucket 服务器
- grep - 如何使用 shell 脚本在文件中查找“>”?
- wordpress - 在 cpanel 中安装 word-press 后,它将自动重定向到 mydomain.in/wp-admin/setup-config.php
- r - RShiny 中的嵌套 uiOutput