0

Hi wordpress community,

What I am trying to do is programmatically create an updated draft (not live) of a published post - so that the changes will be recorded but not published.

Right now, my code creates a form with which the user to update a few fields of a custom post type called "classifieds" from the frontend, but updates the live version of the post, as opposed to saving the new version as a draft.

My end goal is to allow the user to update his/her published or unpublished classified post(s) and save the updates as a draft that the changes can be reviewed by an admin before it gets published - without changing the live post (if it is live).

Thanks :)

edit-post.php template:

<?php
$user = wp_get_current_user()->data;
$user_id = $user->ID;

$query = new WP_Query(array(
    'post_type' => 'classified',
    'post_status' => 'all',
    'author' => $user_id,
    // 'posts_per_page' => -1
));

echo "<h1>Edit your pending classifieds</h1><br>";
echo "<p>The following posts are your pending classifieds. After you edit a classified, it will be reviewed by our team before publishing.</p>";
?>
<div class="accordion" id="post-listing"><?php
    while ($query->have_posts()) {

        $query->the_post();

        // process form
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['postcontent']) ) {

            // redirect header location based on post status
            $post_status = $_POST['post_status'];
            $post_id     = $_POST['post_id'];

            if ($post_status == 'publish') {
                header('Location: /classified/' . $post_id);
            } else { // if $post_type == 'draft' or 'pending'
                header('Location: ?post_type=classified&p=' . $post_id);
            }

            $post = array(
                'ID'             => $post_id,
                'post_content'   => esc_sql($_POST['postcontent']),
                'post_title'     => esc_sql($_POST['post_title']),
                'post_status'    => 'pending'
            );

            wp_update_post($post);

            if ( isset($_POST['owner_name']) ) update_post_meta($post_id, 'owner_name', esc_sql($_POST['owner_name']) );
            if ( isset($_POST['phone']) ) update_post_meta($post_id, 'phone', esc_sql($_POST['phone']) );
            if ( isset($_POST['address']) ) update_post_meta($post_id, 'address', esc_sql($_POST['address']) );
            if ( isset($_POST['zip_code']) ) update_post_meta($post_id, 'zip_code', esc_sql($_POST['zip_code']) );
            if ( isset($_POST['add_coupon']) ) update_post_meta($post_id, 'add_coupon', esc_sql($_POST['add_coupon']) );
        }


        wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' );


        $fields = get_post_meta($post->ID);

        $name    = $fields["owner_name"][0];
        $phone   = $fields["phone"][0];
        $address = $fields["address"][0];
        $zip     = $fields["zip_code"][0];
        $coupon  = $fields["add_coupon"][0];

        var_dump($fields);

        ?>
        <div class="accordion-navigation step-wrapper">
            <a class="step-heading" href="#" onclick="toggle(edit_post_<?php echo $post->ID; ?>);"><span style="float: none;max-width: unset;text-align: left;padding-left: 26px;"><h4 style="margin: 0">Edit <?php echo $post->post_title; ?><?php echo $post->post_status != 'publish' ? ' - draft' : ''; ?></h4></span></a>
            <div class="step-post content  clearfix" id="edit_post_<?php echo $post->ID; ?>" href="#">
                <div class="step-post content  clearfix current">
                    <form id="post" class="dropzone form_front_style" method="post" enctype="multipart/form-data">

                        <input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
                        <input type="hidden" name="post_status" value="<?php echo $post->post_status; ?>" />

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Title</label>
                        <input class="textfield" type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></div>

                        <h3>Classified Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="postcontent">Detail</label>
                        <span style="color: #333"><?php wp_editor( $post->post_content, 'postcontent' ); ?></span></div>

                        <h3>Seller Contact Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="name">Owner Name</label>
                        <input class="textfield" type="text" id="name" name="owner_name" value="<?php echo $name; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="phone">Phone</label>
                        <input class="textfield" type="text" id="phone" name="phone" value="<?php echo $phone; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="address">Address</label>
                        <input class="textfield" type="text" id="address" name="address" value="<?php echo $address; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="zip_code">Zip</label>
                        <input class="textfield" type="text" id="zip_code" name="zip_code" value="<?php echo $zip; ?>" /></div>

                        <h3>Coupons</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="add_coupon">Add Coupon</label>
                        <input class="textfield" <?php echo $coupon ? "disabled style='background-color:#f8f8f8;color:#bdbcbc;'" : ""; ?> type="text" id="add_coupon" name="add_coupon" value="<?php echo $coupon; ?>" /></div>

                        <input class="textfield" type="submit" id="submit" value="Update" />
                    </div>
                </form>
            </div>
        </div>
    <?php
    }
    ?>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.