0
votes

I have created a form using wordpress which returns the title and description. I need to return a few more custom fields. I have searched the internet and found many answers which did not explain it very well and they did not end up working for me. The form would return a post with title, description, the custom fields already filled in. Thanks!

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter some notes';
}


// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title'    =>  $title,
'post_content'  =>  $description,
'post_Blog_URL' => $URL,  
'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
'post_type' =>  'post'  //'post',page' or use a custom post type if you want to
);

add_post_meta($post_id, $meta_key, 'URL' , $unique); 

//SAVE THE POST
$pid = wp_insert_post($new_post);

//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

do_action('wp_insert_post', 'wp_insert_post');

Submit your own!

                <div id="postbox">

                <form id="new_post" name="new_post" method="post" action="" class="" enctype="multipart/form-data">

                <!-- post name -->
                <fieldset name="name">
                    <label>Name of the Article</label>
                    <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
                </fieldset>   


                <!-- post Content -->
                <fieldset class="content">
                    <label for="description">Description</label>
                    <textarea id="description" tabindex="15" name="description"></textarea>
                </fieldset>


                <button type="submit">Submit</button>

                <input type="hidden" name="action" value="new_post" />

                <?php wp_nonce_field( 'new-post' ); ?>

                </form>
1

1 Answers

0
votes

If I understand you correctly, you want to be able to show Post Meta Fields in your form.

There are a few functions to set, update and retrieve these which will be helpful:

get_post_meta or get_post_custom

add_post_meta

update_post_meta

With those functions, you can create any kind of meta information for posts and display them either in the admin panel or to the end user, depending on the scenario.