Skip to content
Advertisement

Custom Page Template Deleted after generating new custom post from Form

I have a custom post type and a custom page template with a form field. The user enters the data and custom metadata in the form field, submits it, a new custom post is generated with the values passed to it. It all works fine except for one thing. When you submit the data it deletes the custom page template. I’ve no idea why this is happening and would appreciate the help in finding where the problem is and how to fix it.

/* Template Name: Add Business */
global $nameError, $websiteError, etc...;

if(isset($_POST['submitted'])) {
    if(trim($_POST['bus_name']) === '') {
        $nameError = 'Please enter your business name.';
        $hasError = true;
    } else { $bus_name = trim($_POST['bus_name']); }

    if(trim($_POST['bus_website']) === '')  {
        $websiteError = 'Please enter the website address for your business.';
        $hasError = true;
    } else { $bus_website = trim($_POST['bus_website']); }  

    etc...

    if(!isset($hasError)) {
        $business_listing       = array(
            'post_type'             => 'business',
            'post_author'           => 1,
            'post_title'            => $bus_name,
            'post_content'          => $bus_desc,
            'post_status'           => 'pending'
        );

        $post_id = wp_update_post( $business_listing );

        add_post_meta($post_id, 'bus_website', $bus_website, true);
        etc...

HTML

<div class="entry-content"><?PHP 
    if(isset($emailSent) && $emailSent == true) { ?>
        <div class="thanks">
            <p>Thanks, your submission was sent successfully. Please allow up to 48 hours for approval.</p>
        </div><?PHP 
        } else { 
            the_content();
            if(isset($hasError) || isset($captchaError)) { ?>
                <p class="error">Sorry, an error occured.<p><?PHP 
            } ?>
            <form action="<?php the_permalink(); ?>" id="bus_form" method="post">
                <ul class="bus_form">
                    <li>
                        <label for="bus_name">Business Name:</label>
                        <input type="text" name="bus_name" id="bus_Name" value="" />
                        <?php if($nameError != '') { ?>
                            <span class="error"><?PHP $nameError; ?></span>
                        <?php } ?>                                          
                    </li>
                    <li>
                        <label for="bus_website">Website Address:</label>
                        <input type="text" name="bus_website" id="bus_website" value="" />
                        <?php if($websiteError != '') { ?>
                            <span class="error"><?PHP $websiteError; ?></span>
                        <?php } ?>                                                                  
                    </li>
                    etc...
                </ul>
            </form>
            <?php } ?>  

Advertisement

Answer

Got it working by…..

Changing – $post_id = wp_update_post( $business_listing );

To – $post_id = wp_insert_post( $business_listing );

And Added:

`add_post_meta($post_id, 'wp_insert_post', 0, true);
 If($post_id !== 0 && !is_wp_error($post_id)){
        
        $business_listing['ID'] = intval($post_id);         
        $post_up = wp_update_post($business_listing);

        add_post_meta($post_id, 'wp_update_post', 0, true);
        add_post_meta($post_id, 'bus_street', $bus_street, true);
        add_post_meta($post_id, 'bus_city', $bus_city, true);
        etc...
        }elseif(is_wp_error($post_id)){
            $error_string = $post_id->get_error_message();
            echo 'ERROR: '.$error_string;
        }`
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement