猿问

每当创建另一个自定义帖子类型时创建自定义帖子类型

我正在尝试向我的 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 天了。


幕布斯7119047
浏览 132回答 1
1回答

饮歌长啸

对于自定义帖子类型,我们必须使用像 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');请试试这个。如果有任何疑问,请随时询问。
随时随地看视频慕课网APP
我要回答