• Resolved emirhangms

    (@emirhangms)


    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

    
    $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

    When i publish and see on site:
    img 2 link

    Note: wp_insert_post works just fine when change post_type to post

    • This topic was modified 3 years, 9 months ago by emirhangms.
    • This topic was modified 3 years, 9 months ago by emirhangms.
    • This topic was modified 3 years, 9 months ago by emirhangms.
    • This topic was modified 3 years, 9 months ago by emirhangms.
    • This topic was modified 3 years, 9 months ago by emirhangms.
    • This topic was modified 3 years, 9 months ago by emirhangms.
    • This topic was modified 3 years, 9 months ago by emirhangms.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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

    (@tw2113)

    The BenchPresser

    Any news or changes here @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.