Wordpress 功能 wp_set_object_term 未设置条款

Wordpress function wp_set_object_term not setting terms

我正在尝试将新的 post 插入到 wordpress 中,post 是一个 woocommerce 产品,但我正在尝试将条款设置为需要的内容。我确实得到了很好的 return 值,但它没有插入到数据库中。

我的代码:

    // Set the page ID so that we know the page was created successfully
    $my_post = array(
        'post_title'        =>  $_POST['post_title'],
        'post_author'       =>  $_POST['current_user'],
        'post_content'      =>  '',
        'comment_status'    =>  'closed',
        'ping_status'       =>  'closed',
        'post_status'       =>  'pending',
        'post_type'         =>  'product'
    );
    $my_post_id = wp_insert_post($my_post, true);

    var_dump($my_post_id);

    if($my_post_id == 0) {
        //Obviously something went wrong
    } else {
        add_post_meta($my_post_id, '_auction_start_price', $_POST['prijs_start']);
        add_post_meta($my_post_id, '_auction_type', 'reverse');
        add_post_meta($my_post_id, '_auction_item_condition', 'new');
        add_post_meta($my_post_id, '_auction_dates_from', $_POST['start_auction']);
        add_post_meta($my_post_id, '_auction_dates_to', $_POST['end_auction']);
        add_post_meta($my_post_id, '_auction_bid_increment', '25');
        $tvar = wp_set_object_terms($new_post_id, 'auction' ,'product_type');
        $tvar2 = wp_set_object_terms($new_post_id, intval($_POST['cat_id']), 'product_cat');
        if(is_wp_error( $tvar )){
            echo 'ERROR';
        }
        var_dump($tvar);
        var_dump($tvar2);
    }

什么是 $tvar 和 $tvar2 returns :

int(908) //Post ID
array(1) { [0]=> string(2) "48" } //The Auction TermID
array(1) { [0]=> string(2) "57" } //The Category TermID

仍然没有在数据库中设置任何想法吗?

我没有注意到您的代码中有任何对 $new_post_id 的引用,因此以下内容不正确

$tvar = wp_set_object_terms($new_post_id, 'auction' ,'product_type');
$tvar2 = wp_set_object_terms($new_post_id, intval($_POST['cat_id']), 'product_cat');

应该是

$tvar = wp_set_object_terms($my_post_id, 'auction' ,'product_type');
$tvar2 = wp_set_object_terms($my_post_id, intval($_POST['cat_id']), 'product_cat');