• Hello!

    I’m working with custom post types for the first time. Love it! I’ve figured out how to add a post type along with a custom meta field. But I can’t get the meta data to save when updating the page. I’m overlooking something in the code but can’t quite see what the deal is. Here is the code I’m using…

    /*Custom META Boxes for Ranch Custom Post Types*/
    /*Ranch Location META Box*/
    add_action( 'add_meta_boxes', 'ranch_location_meta' );
    function ranch_location_meta() {
        add_meta_box(
            'ranch_location_meta',
            __( 'Ranch Location', 'myplugin_textdomain' ),
            'ranch_location_meta_content',
            'ranch',
            'side',
            'high'
        );
    }
    
    function ranch_location_meta_content( $post ) {
        wp_nonce_field( plugin_basename( __FILE__ ), 'ranch_location_meta_content_nonce' );
        echo '<label for="ranch_location"></label>';
        echo '<input type="text" id="ranch_location" name="ranch_location" placeholder="Enter Ranch Location Here" />';
    }
    
        add_action( 'save_post', 'ranch_location_save' );
    
        function ranch_location_save( $post_id ) {
    
            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                return;
    
            if ( !wp_verify_nonce( $_POST['ranch_location_meta_content_nonce'], plugin_basename( __FILE__ ) ) )
                return;
    
            if ( 'page' == $_POST['post_type'] ) {
                if ( !current_user_can( 'edit_page', $post_id ) )
                    return;
            } else {
                if ( !current_user_can( 'edit_post', $post_id ) )
                    return;
            }
            $ranch_location = $_POST['ranch_location'];
            update_post_meta( $post_id, 'ranch_location', $ranch_location );
    
    }

    Any and all guidance would be so appreciated.

    Thanks!

  • The topic ‘Custom Post Type Meta Data Not Saving’ is closed to new replies.