1

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="johndoe@xyz.com" 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.

1 Answer 1

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.org/Function_Reference/update_post_meta

Taxonomies are meant for categories and tags like objects etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.