admin管理员组

文章数量:1313822

I am creating a custom post type with custom taxonomies while submitting front-end form for custom post it is creating a post but it is not creating new taxonomies which I have already set just need to add custom taxonomy

<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');
}

Can anyone help me out for submitting this fields value as custom taxonomies.

I am creating a custom post type with custom taxonomies while submitting front-end form for custom post it is creating a post but it is not creating new taxonomies which I have already set just need to add custom taxonomy

<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');
}

Can anyone help me out for submitting this fields value as custom taxonomies.

Share Improve this question asked Feb 27, 2018 at 12:10 Dharit SoniDharit Soni 1132 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Use the fourth parameter in wp_set_object_terms() and pass 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);

Though I do find it weird to use taxonomies for name, email and birthdate. I'd recommend to use metadata for this.

See: https://codex.wordpress/Function_Reference/update_post_meta

Taxonomies are meant for categories and tags like objects etc.

本文标签: wpsetobjectterms() not adding new term to custom post and custom taxonomy