繁体   English   中英

Wordpress wp_insert_post 在创建新的自定义帖子时正在创建新的分类术语

[英]Wordpress wp_insert_post is creating new Taxonomy term when creating new custom post

在我的 WordPress v5.5.1 中,我使用公共前端表单来创建自定义帖子类型。 我使用以下代码将表单数据保存为自定义帖子:

function save_function() {
   // MESSAGE FIELDS
    $public_post = array(
        'post_title' => filter_input(INPUT_POST, 'title'),
        'post_author' => 1,
        'post_type' => 'message',
        'post_status' => 'pending',
        'tax_input' => array(
            'my_custom_taxonomy' => array(filter_input(INPUT_POST, 'subject')) // subject is a HTML select which captures option value contains term_id which is generated using get_terms.
        )
    );

    wp_insert_post($public_post);
}

它不是将'tax_input'标记为现有的 custom_taxonomy 术语,而是创建一个重复的术语,并将term_id作为术语名称。

你好,请试试这个功能

   function save_function()
{

    $subject_term = 'subject';
    $my_subject_term = term_exists($subject_term, 'my_custom_taxonomy');   // check if term in website or no
    // Create Term if it doesn't exist
    if (!$my_subject_term) {
        $my_subject_term = wp_insert_term($subject_term, 'my_custom_taxonomy');
    }
    $custom_tax = array(
        'my_custom_taxonomy' => array(
            $my_subject_term['term_taxonomy_id'],
        )
    );

    // MESSAGE FIELDS
    $public_post = array(
        'post_title' => filter_input(INPUT_POST, 'title'),
        'post_author' => 1,
        'post_type' => 'message',
        'post_status' => 'pending',
        'tax_input' => $custom_tax
    );

    $post_id = wp_insert_post($public_post);

    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM