wp_set_object_terms不使用自定义分类法和cpt

时间:2016-07-23 08:52:56

标签: php wordpress

我构建了一个允许用户从前端发布的表单。 我试图通过wp_set_object_terms传递分类法,因为我使用自定义帖子类型和自定义分类。 这是我的代码:

if(isset ($_POST['submit_offer'])=='submit'){
     $user_id = get_current_user_id();
    $args=array(
        'post_author'   => $user_id,
        'post_title'    => $_POST['job_title'],
        'post_content'  => $_POST['post_content'],
        'post_excerpt'  => $_POST['job_field'],
        'meta_input'    => array(
                            'points_amount' => $_POST['post_points'],              
                                ),
        'post_status'   => 'publish',
        'post_type'     => 'job_offers',

    );

    // get post id
    $post_id = wp_insert_post($args);
  $job_tax = array ( 44 , 45 );
  wp_set_object_terms( $post_id , $job_tax, 'field_of_work' );
}

并且它无法正常工作!!!!!

2 个答案:

答案 0 :(得分:3)

所以,终于解决了。只需要将表单包装在一个函数中并将其添加到init。 :

<? php
function job_offer_form_init() {

// form to post test

function job_offer_form() {
$user_id = get_current_user_id();
$job_field_1 = userpro_profile_data('main_occupation', $user_id);
$job_field_2 = userpro_profile_data('sub_occupation_01', $user_id);
$job_field_3 = userpro_profile_data('sub_occupation_02', $user_id);
$form ='<div class="form-container">
<form action="" method="post" role="form">
<legend>הצעת שירותים/מוצרים</legend>

<div class="form-group">
    <label for="job_title">כותרת ההצעה</label></br>
    <input type="text" class="form-control" name="job_title"  placeholder=""></br>
    <label for="post_content">תוכן ההצעה</label></br>
    <textarea class="form-control" name="post_content"  placeholder=""></textarea></br>
    <label for="job_field">תחום ההצעה</label></br>
    <select name="job_field" multiple>
        <option value="'.$job_field_1.'" selected>'.$job_field_1.'</option>
        <option value="'.$job_field_2.'">'.$job_field_2.'</option>
        <option value="'.$job_field_3.'">'.$job_field_3.'</option>
    </select></br>
    <label for="post_points">עלות בנקודות</label></br>
    <input type="number" class="form-control" name="post_points"  placeholder=""></br>
</div>

<button type="submit" name="submit_offer" value="submit" class="btn btn-primary">הגשת הצעה</button>
</form></div>' ;
return $form;
}
add_shortcode( 'jobofferform', 'job_offer_form' );


if(isset ($_POST['submit_offer'])=='submit'){
 $user_id = get_current_user_id();
$args=array(
    'post_author'   => $user_id,
    'post_title'    => $_POST['job_title'],
    'post_content'  => $_POST['post_content'],
    'post_excerpt'  => $_POST['job_field'],
   /* 'tax_input'       => array(
                            'field_of_work' => array(44, 45),
                         ), */
    'meta_input'    => array(
                        'points_amount' => $_POST['post_points'],
                      //  'job_field'   => $_POST['job_field'],

                            ),
    'post_status'   => 'publish',
    'post_type'     => 'job_offers',

);

// get post id
$post_id = wp_insert_post($args);
$job_tax = array ( 48 , 49 );
$term_taxonomy_ids=wp_set_object_terms( $post_id , $job_tax, 'field-of-work' , true );

if (is_wp_error($term_taxonomy_ids )) {
$error_string= $term_taxonomy_ids -> get_error_message();
add_post_meta( $post_id, 'taxonomies-error', $error_string );
} else {
add_post_meta( $post_id, 'taxonomies-error', 'no error' );
}

}
}

 add_action( 'init', 'job_offer_form_init', 0 );
  ?>

答案 1 :(得分:0)

对于那些具有自定义帖子类型和自定义分类标准并且其代码位于functions.php中的用户,请确保为插入帖子的函数赋予低优先级(20)。这样就可以了!
这是 functions.php中 wp_insert_post()的一种方法:

function insert_db_posts() {
        $thepost = wp_insert_post(array(
                'post_type'         =>  'art',
                'post_title'        =>  'Try hard',
                ));
        wp_set_object_terms( $thepost, 37, 'artist');
}
add_action( 'init', 'insert_db_posts', 20 ); // this will fire very late!!!