我有一個自定義帖子型別作業和一個自定義分類jobCategory。wp_insert_post() 并且它有效。當用戶選擇多個jobCategory時,資料庫中只記錄一個選擇的類別。請指導我。
if (isset($_POST['send'])) {
$post_id = wp_insert_post(array(
'post_type' => 'jobs',
'post_author' => get_current_user_id(),
'post_title' => $_POST['title'],
'post_content' => $_POST['description'],
'post_status' => 'draft',
));
wp_set_object_terms($post_id, intval($_POST['category']), 'jobCategory');
add_post_meta($post_id, 'price', $_POST['price']);
add_post_meta($post_id, 'time', $_POST['time']);
if (isset($_FILES['file'])) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
$uploadedfile = $_FILES['file'];
$upload_overrides = array(
'test_form' => false
);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
add_post_meta($post_id, 'file', $movefile['url']);
add_post_meta($post_id, 'payToSee', 0);}
wp_redirect('my-ads');
}
<div id="wpap-content"> <div class="col-12"> <div class="card"> <div class="card-body"> <form action="" method="POST" enctype="multipart/form-data"> <div class="form-group"> <label for="title"> Title </label> <input required type="text" id="title" name="title" class="form-control"> </div><div class="form-group"> <label for="description"> details </label> <textarea name="description" id="description" class="form-control" cols="30" rows="10"></textarea> </div><div class="form-group"> <label for="category"> category </label> <select id="js-choice" id="category" name="category[]" multiple="multiple"> <?php $terms=get_terms([ 'taxonomy'=> 'jobCategory', 'hide_empty'=> false,]); foreach ($terms as $cat){?> <option value="<?=$cat->term_id ?>"><?=$cat->name ?></option> <?php}?> </select> </div><div class="form-group"> <label for="price"> Price </label> <input required type="text" id="price" name="price" class="form-control"> </div><div class="form-group"> <label for="time"> Time </label> <input required type="text" id="time" name="time" class="form-control"> </div><div class="form-group"> <label for="file"> Files </label> <input type="file" id="file" name="file" class="form-control"> </div><button value="1" name="send" type="submit" class="btn text-center btn-primary">Submit </button> </form> </div></div>
uj5u.com熱心網友回復:
有問題的行是:
wp_set_object_terms($post_id, intval($_POST['category']), 'jobCategory');
您對陣列執行 intval 并獲得第一個值。
您可以使用 轉換值array_map。
$categories = array_map('intval', $_POST['category']);
wp_set_object_terms($post_id, $categories, 'jobCategory');
我還建議在未經用戶過濾的情況下不要獲取資料。您需要使用過濾器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435971.html
下一篇:如何在帖子上顯示類別和日期
