Front end posting overwriting custom meta fields
-
I’ve created this page in my theme which allows users to edit their posts. It changes the information which there are fields for in the page BUT it overwrites all the other custom meta fields which are not in the edit page. Any ideas of where I’m going wrong?
<?php /* Template Name: Edit Posts2 */ $query = new WP_Query(array('post_type' => 'customtype’, 'posts_per_page' =>'-1', 'post_status' => array('publish', 'pending', 'draft') ) ); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); if(isset($_GET['post'])) { if($_GET['post'] == $post->ID) { $current_post = $post->ID; $title = get_the_title(); $content = get_the_content(); $custom_one = get_post_meta($current_post, 'custom_meta_1', true); $custom_two = get_post_meta($current_post, 'custom_meta_2', true); $custom_three = get_post_meta($current_post, 'custom_meta_3', true); $custom_four = get_post_meta($current_post, 'custom_meta_4', true); $custom_five = get_post_meta($current_post, 'custom_meta_5', true); } } endwhile; endif; wp_reset_query(); global $current_post; $postTitleError = ''; if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) { if(trim($_POST['postTitle']) === '') { $postTitleError = 'Please enter a title.'; $hasError = true; } else { $postTitle = trim($_POST['postTitle']); } $post_information = array( 'ID' => $current_post, 'post_title' => esc_attr(strip_tags($_POST['postTitle'])), 'post_content' => esc_attr(strip_tags($_POST['postContent'])), 'post-type' => 'customtype', 'post_status' => 'publish' ); $post_id = wp_update_post($post_information); if($post_id) { // Update Custom Meta update_post_meta($post_id, 'custom_meta_1', esc_attr(strip_tags($_POST['customMetaOne']))); update_post_meta($post_id, 'custom_meta_2', esc_attr(strip_tags($_POST['customMetaTwo']))); update_post_meta($post_id, 'custom_meta_3', esc_attr(strip_tags($_POST['customMetaThree']))); update_post_meta($post_id, 'custom_meta_4', esc_attr(strip_tags($_POST['customMetaFour']))); update_post_meta($post_id, 'custom_meta_5', esc_attr(strip_tags($_POST['customMetaFive']))); wp_redirect( home_url().'/?p=56' ); exit; } } ?> <?php get_header(); ?> <!-- #primary BEGIN --> <div id="primary"> <form action="" id="primaryPostForm" method="POST"> <fieldset style="border:none;"> <label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label> <input type="text" name="postTitle" id="postTitle" value="<?php echo $title; ?>" class="required" /> </fieldset> <?php if($postTitleError != '') { ?> <span class="error"><?php echo $postTitleError; ?></span> <div class="clearfix"></div> <?php } ?> <fieldset style="border:none;"> <label for="customMetaFour"><?php _e(‘Custom 4:’, 'framework') ?></label> <input type="text" name="customMetaFour" id="customMetaFour" value="<?php echo $custom_four; ?>" /> </fieldset> <fieldset style="border:none;"> <label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label> <textarea name="postContent" id="postContent" rows="8" cols="30"><?php echo $content; ?></textarea> </fieldset> <fieldset style="border:none;"> <label for="customMetaOne"><?php _e(‘Custom 1:’, 'framework') ?></label> <input type="text" name="customMetaOne" id="customMetaOne" value="<?php echo $custom_one; ?>" /> </fieldset> <fieldset style="border:none;"> <label for="customMetaTwo"><?php _e(‘Custom 2:’, 'framework') ?></label> <input type="text" name="customMetaTwo" id="customMetaTwo" value="<?php echo $custom_two; ?>" /> </fieldset> <fieldset style="border:none;"> <label for="customMetaThree"><?php _e(‘Custom 3:’, 'framework') ?></label> <input type="text" name="customMetaThree" id="customMetaThree" value="<?php echo $custom_three; ?>" /> </fieldset> <b> Booking Toggle:</b> </br> <fieldset style="border:none;"> <span class="toggle"> <input type="checkbox" name="customMetaFive" id="customMetaFive" value="checked" <?php if($custom_five=="checked"){echo "checked";} ?> /> <label for="customMetaFive" data-off="OFF" data-on="ON"></label> </span> </fieldset> <fieldset style="border:none;"> <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?> <input type="hidden" name="submitted" id="submitted" value="true" /> <button type="submit"><?php _e('Update Post', 'framework') ?></button> </fieldset> </form> </div><!-- #primary END --> <?php get_footer(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Front end posting overwriting custom meta fields’ is closed to new replies.