WordPress.org

Forums

Front end posting overwriting custom meta fields (5 posts)

  1. mortonc
    Member
    Posted 1 year ago #

    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(); ?>
  2. mortonc
    Member
    Posted 1 year ago #

    Any thoughts?

  3. mortonc
    Member
    Posted 1 year ago #

    Could anybody help? I would really appreciate it?

  4. mortonc
    Member
    Posted 1 year ago #

    Sorry to keep bumping but could really do with a little help.

    Thanks

  5. frenchy black
    Member
    Posted 9 months ago #

    your code is setup wrong try this code below, add your codes before the header.

    <?php
    /*
    Template Name: Page Testimonials
    */

    $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']);
    }
    $eventurl = $_POST['subtitle'];

    $post_information = array(
    'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
    'post_content' => esc_attr(strip_tags($_POST['postContent'])),
    'post_type' => 'testimonial',
    'post_status' => 'pending',
    'subtitle' => $eventurl,
    );

    $post_id = wp_insert_post($post_information);

    //save the new post
    add_post_meta($post_id, 'subtitle', $eventurl, true);

    if($post_id)
    {
    // Update Custom Meta
    update_post_meta($post_id, 'subtitle', esc_attr(strip_tags($_POST['eventurl'])));

    // Redirect
    wp_redirect(home_url('testimonials'));
    exit;
    }

    }

    ?>

    <?php get_header(); ?>

    <div class="content ">
    <div class="container">

    <div class="main-content">

    <div class="clear"></div>

    </div><!--- end of .main-content -->

    <div class="sidebar">

    <div class="form-container clear">
    <div id="wpcf7-f272-o1" class="wpcf7">
    <form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">

    <header class="header-entry">
    <h2>do you have a story?</h2>
    <h3>share your testimony to encourage others</h3>
    </header>
    <p>First Last Name (required)
    <span class="wpcf7-form-control-wrap your-name">
    <fieldset>
    <!-- label for="postTitle"><?php// _e('Post\'s Title:', 'framework') ?></label-->

    <input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required wpcf7-form-control wpcf7-text wpcf7-validates-as-required" placeholder="" />

    </fieldset>

    <?php if($postTitleError != '') { ?>
    <span class="error"><?php echo $postTitleError; ?></span>
    <div class="clearfix"></div>
    <?php } ?>

    </span> </p>

    <p>City, State
    <span class="wpcf7-form-control-wrap CityState">
    <!-- Event Url -->
    <fieldset class="eventurl">
    <input type="text" value="" id="eventurl" class="wpcf7-form-control wpcf7-text" size="40" name="eventurl" placeholder="">
    </fieldset>
    </span> </p>

    <p>testimony
    <span class="wpcf7-form-control-wrap your-message">
    <fieldset>

    <textarea name="postContent" id="postContent" class="wpcf7-form-control wpcf7-textarea" rows="10" cols="40" placeholder="Write a testimonial here. 300 charactors max."><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>

    </fieldset>
    </span> </p>
    <p>
    </fieldset>

    <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>

    <input type="hidden" name="submitted" id="submitted" value="true" />
    <button type="submit" class="submitevent wpcf7-form-control wpcf7-submit"><?php _e('Submit', 'framework') ?></button>

    </fieldset>
    </p>
    <p>maximum 300 characters</p>
    <p>clear</p>
    </form>
    </div>
    </div>

    </div><!--- end of .sidebar -->

    <div class="clear"></div>

    </div><!--- end of .container -->
    </div><!--- end of .content -->

    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic