> For the complete documentation index, see [llms.txt](https://learnphp.gitbook.io/learnphp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnphp.gitbook.io/learnphp/wordpress-advand/insert-post-with-custom-taxonomy-ok.md).

# Insert post with custom taxonomy 🌟(ok)

https\://wordpress.stackexchange.com/questions/210229/tax-input-not-working-wp-insert-post

**Sử dụng wp\_insert\_post**

C:\xampp\htdocs\style2\wp-content\themes\gutener\header.php

```
<?php  
    $custom_tax = array(
      'subjects' => array(73,72)
    );
    $post = array(
        'post_content' => 'This will be the content.',
        'post_title'   => 'Hello World 1',
        'tax_input'    => $custom_tax
    );
    $post_id = wp_insert_post( $post );
  ?>
```

C:\xampp\htdocs\style2\wp-content\themes\gutener\functions.php

```
//create a custom taxonomy name it subjects for your posts
function create_subjects_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
  $labels = array(
    'name' => _x( 'Subjects', 'taxonomy general name' ),
    'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Subjects' ),
    'all_items' => __( 'All Subjects' ),
    'parent_item' => __( 'Parent Subject' ),
    'parent_item_colon' => __( 'Parent Subject:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Subjects' ),
  );    
// Now register the taxonomy
  register_taxonomy('subjects',array('post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'subject' ),
  ));
}
```

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FdpZAzWypG5Sm1dWJKFMD%2FScreenshot_7.png?alt=media\&token=00ee8b4e-5e24-4905-910d-545725937230)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FrB7QMzssSSXXZ3ZscPH8%2FScreenshot_8.png?alt=media\&token=a2912643-78ae-4bae-b3f2-6b4fa598189c)

**Sử dụng wp\_update\_post**

C:\xampp\htdocs\style2\wp-content\themes\gutener\header.php

```
<?php  
    $custom_tax = array(
      'subjects' => array(73,72)
    );
    // $post = array(
    //     'post_content' => 'This will be the content.',
    //     'post_title'   => 'Hello World 1',
    //     'tax_input'    => $custom_tax
    // );
    // $post_id = wp_insert_post( $post );
     $custom_tax = array(
      'subjects' => array(73)
    );
    $my_post = array(
      'ID'           => 2512,
      'post_title'   => 'This is the post title.',
      'post_content' => 'This is the updated content.',
      'tax_input'    => $custom_tax
    );
    // Update the post into the database
    wp_update_post( $my_post );
  ?>
```

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2Fsi8ZkXrpcrgFfhTVadlK%2FScreenshot_9.png?alt=media\&token=8df9276a-eb74-4236-b5c7-d9f82606df76)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2F16VnpTrPOBTn94KshJwk%2FScreenshot_10.png?alt=media\&token=800cb74c-0448-48fa-af15-508ecd8a3d5e)
