我正在使用自定义分类法创建自定义帖子类型,同时为自定义帖子提交前端表单。它正在创建帖子,但并没有创建我已经设置的新分类法,只需要添加自定义分类法
<form action=\'\' method="post" id="drop_message">
<div class="input-group">
<div class="first">
<label for="first-name-opt">First Name:</label>
<input type="text" name="name" id="name" placeholder="John Doe" class="tag-msg">
</div>
<div class="middle">
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="[email protected]" class="tag-msg">
</div>
<div class="last">
<label for="birthdate">Date Of Birth:</label>
<input type="text" name="birthdate" id="birthdate" placeholder="DD - MM - YYYY" class="tag-msg">
</div>
<div class="submit">
<input type="submit" name="submit" id="submit" value="Leave a Message">
</div>
</div>
</form>
<?php
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\']) {
$name = $_POST[\'name\'];
$email = $_POST[\'email\'];
$birthdate = $_POST[\'birthdate\'];
$post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_author\' => $user,
\'post_status\' => \'publish\',
\'post_type\' => \'messages\',
\'tax_input\' => array(
$name,
$email,
$birthdate,
),
);
$pid = wp_insert_post( $post );
wp_set_object_terms( $pid, $name, \'name\');
wp_set_object_terms( $pid, $email, \'email\');
wp_set_object_terms( $pid, $birthdate, \'dob\');
}
任何人都可以帮助我将此字段值作为自定义分类法提交。
SO网友:Jory Hogeveen
使用中的第四个参数wp_set_object_terms()
并通过true
.
wp_set_object_terms( $pid, $name, \'name\', true);
wp_set_object_terms( $pid, $email, \'email\', true);
wp_set_object_terms( $pid, $birthdate, \'dob\', true);
虽然我确实觉得对姓名、电子邮件和生日使用分类法很奇怪。我建议对此使用元数据。
请参见:https://codex.wordpress.org/Function_Reference/update_post_meta
分类法适用于类别和标记,如对象等。