0

I'm trying to figure out how to make it so that as soon as the post-new.php page for a custom post type is loaded, the post is saved. Basically, I'm trying to make sure the post has it's postID saved before anyone even starts entering anything.

I suppose I could probably "fake-click" the "Save Draft" button, but I'm wondering if there's a more legit way to handle it.

0

This should work. It's the way press-this gets the post id when you click the bookbarklet.

add_action( 'load-post-new.php', 'prefix_save_it' );
function prefix_save_it() {
    // define some basic variables
    $quick = array();
    $quick['post_status'] = 'draft'; // set as draft first
    $quick['post_category'] = isset($_POST['post_category']) ? $_POST['post_category'] : null;
    $quick['tax_input'] = isset($_POST['tax_input']) ? $_POST['tax_input'] : null;
    $quick['post_title'] = isset($_POST['post_title'] ? $_POST['title'] : null;
    $quick['post_content'] = isset($_POST['post_content']) ? $_POST['post_content'] : null;

    // insert the post with nothing in it, to get an ID
    $post_ID = wp_insert_post($quick, true);
    if ( is_wp_error($post_ID) )
        wp_die($post_ID);

        $quick['ID'] = $post_ID;
        wp_update_post($quick);
    }
    return $post_ID;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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