0

This is my issue, i hope to find the answer here.

<div class="form-group">
   <label for="genres" class="col-sm-2 control-label">genres</label>
   <div class="col-sm-10">
      <input type="text" class="form-control" id="genres" name="genres">
   </div>
</div>

// Genres value is : anime, action, adventure,

function post_creation() {
    if(isset($_POST['movie_nonce_field']) && wp_verify_nonce($_POST['movie_nonce_field'], 'movie_nonce')) {
        if(strlen(trim($_POST['title'])) < 1 || strlen(trim($_POST['overview'])) < 1) {
            $redirect = add_query_arg('post', 'failed', home_url($_POST['_wp_http_referer']));
        } else {

            $category = array( $_POST['genres'] );

            $post_info = array(
                'post_type' => 'movies',
                'post_status' => 'pending',
                'post_title' => esc_attr(strip_tags($_POST['title'])),
                'post_content' => esc_attr(strip_tags($_POST['overview'])),
                'post_category' => $category,

            );
            $post_id = wp_insert_post($post_info);

            if($post_id) {
                update_post_meta($post_id, 'ecpt_postedby', esc_attr(strip_tags($_POST['user_name'])));
                update_post_meta($post_id, 'ecpt_posteremail', esc_attr(strip_tags($_POST['user_email'])));
                update_post_meta($post_id, 'ecpt_contactemail', esc_attr(strip_tags($_POST['inquiry_email'])));
                $redirect = add_query_arg('post', 'successfull', home_url($_POST['_wp_http_referer']));
            }
        }
        wp_redirect($redirect); exit;
    }
}
add_action('init', 'post_creation');

Categories not save to post.

I want to add new post to categories if it already exist And create new category if it doesn't exist

1
  • any help ?????? Commented Oct 11, 2019 at 11:46

1 Answer 1

0

It looks like you're trying to use the default Post post type's Category taxonomy with your custom post type movies.

$post_info = array(
  'post_type'     => 'movies', // custom post type here
  'post_status'   => 'pending',
  'post_title'    => esc_attr(strip_tags($_POST['title'])),
  'post_content'  => esc_attr(strip_tags($_POST['overview'])),
  'post_category' => $category, // categories are by default only for Post post type
);

Have you already added such code to your site, which allows you to use the default Category taxonomy with your custom post type? If not, have a look here on how to do it, tags & categories with custom post type

If you have registered a custom taxonomy for your movies post type, then you should use tax_input in your $post_info array. Like so,

$post_info = array(
  'post_type'     => 'movies',
  'post_status'   => 'pending',
  'post_title'    => esc_attr(strip_tags($_POST['title'])),
  'post_content'  => esc_attr(strip_tags($_POST['overview'])),
  'tax_input'     => array(
    'some_custom_taxonomy' => $category
  ),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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