Title: Can&#8217;t use wp_insert_post function with custom post type
Last modified: August 25, 2022

---

# Can’t use wp_insert_post function with custom post type

 *  Resolved [emirhangms](https://wordpress.org/support/users/emirhangms/)
 * (@emirhangms)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/can-use-insert_new_post-function-with-custom-post-type/)
 * created a custom post type with CPT plugin. It’s works fine and did the job. 
   I can create post, publish the post and i can see on the site. But when i create
   a post from frontend via wp_insert_post it’s creates the post but i can’t see
   on site. It gives me 404.
 * [stackoverflow](https://stackoverflow.com/questions/73484741/wordpress-wp-insert-post-in-post-type-doesnt-work?noredirect=1#comment129771393_73484741)
 *     ```
       $post_args = array(
           'post_type' => 'm',
           'post_title' => $menuName . ' | ' . $postId,
           'post_name' => "".$postId."",
           'post_content' => '',
           'post_status' => 'publish',
           'post_author' => get_current_user_id(),
       );
   
       $post_id = wp_insert_post($post_args);
       if(!is_wp_error($post_id)){
         echo 'created ' . $post_id;
       } else {
         //there was an error in the post insertion, 
         echo $post_id->get_error_message();
       }
       ```
   
 * When i create a post:
    [img 1 link](https://i.hizliresim.com/56exa3v.jpg)
 * When i publish and see on site:
    [img 2 link](https://i.hizliresim.com/rtrbmkp.jpg)
 * Note: wp_insert_post works just fine when change post_type to post
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).
    -  This topic was modified 3 years, 9 months ago by [emirhangms](https://wordpress.org/support/users/emirhangms/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [3 years, 9 months ago](https://wordpress.org/support/topic/can-use-insert_new_post-function-with-custom-post-type/#post-15950908)
 * Finding it odd that you’re setting the args for wp_insert_post to have a status
   of “publish” but your first screenshot is showing it as draft.
 * Is “m” the post type slug for this?
 * Are you getting any errors?
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [3 years, 8 months ago](https://wordpress.org/support/topic/can-use-insert_new_post-function-with-custom-post-type/#post-16025126)
 * Any news or changes here [@emirhangms](https://wordpress.org/support/users/emirhangms/)?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Can’t use wp_insert_post function with custom post type’ is closed to
new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [3 years, 8 months ago](https://wordpress.org/support/topic/can-use-insert_new_post-function-with-custom-post-type/#post-16025126)
 * Status: resolved