我创建了一个网站,未注册用户可以在其中保存正面的其他广告。现在,它只是测试研究,在这里我尝试将数据插入wp_posts和wp_term_relationships表。
我只是为了测试而硬写了'post_category'=> array(3)。

if( isset($_POST['submit']) &&  wp_verify_nonce( $_POST['name_of_nonce_field'], 'name_of_my_action'  )){
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter the content';
}
$post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_status'   => 'pending',
    'post_type' => 'category',
    'post_category' => array(3),
);
wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
wp_redirect( home_url() );
exit;
}


html形式:

<form  name="new_post" method="post" action="">
        <p><label class="form__title" for="title"></label><br />
            <input class="form__title-input" type="text"  size="20" name="title" />
        </p>
        <div class="form-group">
            <label for="exampleSelect1">Example select</label>
            <select class="form-control" id="exampleSelect1" name="category">
                <?php foreach ($terms as $el) :?>
                    <?php echo "<option>". $el->name. "</option>" ?>
                <?php endforeach; ?>
            </select>
        </div>
        <?php foreach ($customTags as $el) :?>
        <div class="form-check form-check-inline">
    <?php echo '<input class="form-check-input" name"check_list" type="checkbox" id="'.$el->term_id. '" value="ZAGlushKa">'?>
    <?php echo '<label class="form-check-label" for="inlineCheckbox1">'.$el->name.'</label>'?>
        </div>
        <?php endforeach; ?>
        <p><label class="form__title" for="description">About You</label><br />
            <textarea class="form__content-area"  name="description" cols="50" rows="6"></textarea>
        </p>
        <p><label class="form__title"></label><br />
            <input class="form__title-input" type="text" value=""  size="16" name="post_tags"/>
        </p>
        <p><input class="form__submit" type="submit" value="Publish" name="submit" /></p>
        <input type="hidden" name="post_type" id="post_type" value="professionals" />
        <input type="hidden" name="action" value="post" />
        <?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' );?>
    </form>


我只想将帖子与wp_term_relationships表中的关系保存在wp_posts表中。

最佳答案

$ post数组的post_type应该是post

关于php - 保存新帖子时,无法在wp_term_relationships表中插入我的自定义类别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49494923/

10-10 15:29