1

The code below works when I am inserting a post/using add_post_meta when submitting a form. The page refresh is flawless (see How can I fix a slow redirect after form submit from frontend (no plugin)?). However, I can't seem to get the same effect when using forms that I intend to update the post meta for my custom post type using update_post_meta.

function wpd_check_submission(){
    if( isset( $_POST['start'] ) ) {
        // do your post insertion here
    }
}
add_action( 'init', 'wpd_check_submission' );

Could anyone help point me in the right direction? I can't find a clear answer on this. I've tried using the save_post hook, putting the if-isset lines directly after my form, adding the update_post_meta functions directly into the function that is working. I've started reading about do_action methods for updating post meta that have updated_post_meta and update_post_meta--I'm really confused.

Here's an example: the code that works:

   function ty_save_post_data() {

    if( isset( $_POST['start'] ) ) { 

        $post = array(
        'post_title' => $_POST[ 'mfname' ],
        'post_status' => 'publish', 
        'post_type' => 'add-members' 
        );  


        $post_id = wp_insert_post($post); 

        add_post_meta($post_id, 'mfname', sanitize_text_field( $_POST[ 'mfname' ] ), true);
        add_post_meta($post_id, 'mem', sanitize_text_field( $_POST[ 'mem' ] ), true);
        add_post_meta($post_id, 'mpw', esc_attr( $_POST['mpw'] ), true);
        add_post_meta($post_id, 'second-nom', sanitize_text_field( $_POST[ 'second-nom' ] ), true );
    } 
    }

add_action( 'init', 'ty_save_post_data' );

The code that fails:

function ty_save_post_data1() { 
if( isset( $_POST['isecond'] ) ) { 
   update_post_meta( get_the_ID(), 'second-nom', sanitize_text_field( $_POST['second-nom'] ) );
   update_post_meta( get_the_ID(), 'second-nom-date', sanitize_text_field( $_POST['second-nom-date'] ) );
   update_post_meta( get_the_ID(), 'approved', sanitize_text_field( $_POST['approved'] ) );    
}
}

add_action( 'init', 'ty_save_post_data1' );

HERE IS THE UPDATED WORKING VERSION OF THE FAILED CODE EXAMPLE

//hidden field added to form that grabs the post ID
<input type="hidden" name="post_id" id="post_id" value="<?php echo get_the_ID(); ?>" />

<?php function ty_save_post_data1() { 
if( isset( $_POST['isecond'] ) ) {

   $post_id1 = $_POST['post_id'];

   update_post_meta( $post_id1, 'second-nom', sanitize_text_field( $_POST['second-nom'] ) );
   update_post_meta( $post_id1, 'second-nom-date', sanitize_text_field( $_POST['second-nom-date'] ) );
   update_post_meta( $post_id1, 'approved', sanitize_text_field( $_POST['approved'] ) );    
}
}

add_action( 'init', 'ty_save_post_data1' );
?>
2

The context of your code that fails isn't entirely clear, but it's failing because get_the_ID() won't return a post ID when run on the init hook, because the main query hasn't been executed yet.

If you have a look at the Action Reference, wp is the earliest action where you can access the current query's data, if your code is operating on the currently viewed post. If your code is operating on a different post, you'll need to pass that post's ID along with your other form data.

  • Thank you for pointing me in the right direction! Solved. See edit. Thank you! – WorkingMan8798 May 23 '15 at 16:44

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.