Support » Plugins » Hacks » Cant save custom categories of a custom taxonomy

  • Resolved Manoj Trivand


    I use the code like below in a submit page.

    $new_post = array(
    ‘post_title’ => $title,
    ‘post_content’ => $description,
    ‘post_excerpt’ => $excerpt,
    ‘post_category’ => array($_POST[‘cat’]),
    ‘tags_input’ => array($tags),
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘custom_post_type’
    $pid = wp_insert_post($new_post);

    The $_POST[‘cat’] fetches the correct category id. My custom taxonomy is named directory. I am able to view in the saved post in the backend but the category checkbox is not checked there and in the front end i am displaying the post under these custom category, but it doesnt appear. Is there a way to save the custom category of a custom taxonomy properly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    You cannot use the post_category key for custom taxonomies. When you use that key, the ‘category’ taxonomy is assumed. When inserting the post, don’t worry about your custom terms yet. Have your script keep the resulting post ID for the next step, which is to call wp_set_post_terms() where you can properly associate your custom taxonomy terms with the post.

    I found the solution.. the code is:

    $cat_ids = array( $catid );
    $cat_ids = array_map(‘intval’, $cat_ids);
    wp_set_object_terms( $postid, $cat_ids, ‘my_custom_taxonomy’ );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cant save custom categories of a custom taxonomy’ is closed to new replies.