0

When I call wp_insert_post() with a post status, it only creates posts with "Draft" status. I've tested it with WordPress built-in statuses and my custom post statuses. But it only creates Draft posts, which I can't trash or delete them from WordPress. Here is my code. Please help.

// Prepare to save!
        do_action( 'wpo_frontend_before_save_order', $order_details, $user_details );
        $post = array(
            'post_title'        =>  $order_details['title'],
            'post_author'       =>  get_current_user_id(),
            'post_type'         =>  'wp_order',
            'post_status'       =>  'pending'
        );
        $postID = wp_insert_post( $post );
        if ( is_wp_error( $postID ) ) {
            set_transient( 'wpo_new_order_message_' . get_current_user_id(), __( 'There was a problem to register your order. Please try again later', 'wpo' ) );
            return;
        }
4
  • It's very likely that this is caused by a plugin. Does this problem occur with all plugins enabled? Could you check your plugin folders for the occurence of "wp_insert_post_data"?
    – engelen
    Jun 26 '14 at 10:55
  • I never used "wp_insert_post_data", I never hear it before that.
    – Ehsaan
    Jun 27 '14 at 10:42
  • It might be a plugin that's actually hooking into this filter. Please just simply do a search in your plugins folder and post the results here.
    – engelen
    Jun 27 '14 at 18:43
  • @Ehsaan as a side note, you should try to avoid using $post as a variable name, as it's often a global reference to the WP_Post object
    – FaCE
    Sep 15 '16 at 17:46
1

The reason why your code created drafts only is status='pending' make it publish

Like // Prepare to save!

   do_action( 'wpo_frontend_before_save_order', $order_details, $user_details );
     $post = array(
         'post_title'        =>  $order_details['title'],
         'post_author'       =>  get_current_user_id(),
         'post_type'         =>  'wp_order',
         'post_status'       =>  'publish'
     );
     $postID = wp_insert_post( $post );
     if ( is_wp_error( $postID ) ) {
         set_transient( 'wpo_new_order_message_' . get_current_user_id(), __( 'There was a problem to register your order. Please try again later', 'wpo' ) );
         return;
    }
6
  • In the example, it uses pending, but he did mention that he tried it with all built in statuses, so he would have tried "publish" too... Jun 26 '14 at 13:55
  • Thank you, but I don't have publish status. I can't use it. I must be used "pending" or "unpayed" or "payed" or "completed"
    – Ehsaan
    Jun 27 '14 at 10:40
  • publish didn't work. any ideas? is this a WordPress bug or my plugin bug?
    – Ehsaan
    Jun 27 '14 at 10:44
  • Have your tried with "payed" and "completed"? Jul 2 '14 at 7:13
  • yes TBI. I tested. It doesn't work. Please anyone help me.
    – Ehsaan
    Jul 7 '14 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.