【问题标题】:Wordpress function wp_set_object_term not setting termsWordpress 函数 wp_set_object_term 未设置术语
【发布时间】:2015-08-26 10:27:52
【问题描述】:

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

我的代码:

    // 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 返回什么:

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

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

【问题讨论】:

    标签: php wordpress woocommerce custom-post-type


    【解决方案1】:

    我没有注意到您的代码中对 $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');
    

    【讨论】:

    • 啊该死你的权利,看了大约 100 次,找不到:D 谢谢
    • 我知道并理解这种感觉 :) 很高兴我能提供帮助。
    猜你喜欢
    • 2015-01-13
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多