[resolved] Cant save custom categories of a custom taxonomy (3 posts)

  1. Manoj Trivand
    Posted 2 years ago #

    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.

  2. bcworkz
    Posted 2 years ago #

    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.

  3. Manoj Trivand
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic