• Resolved broadleon

    (@broadleon)


    Hi

    I have created a custom form on the twentyten template, but some of the boxes don’t post to a custom field in the custom post type.

    how would i get the

    <!-- First Name -->
    			<fieldset name="fname">
    				<label for="fname">First Name:</label>
    				<input type="text" id="fname" value="" tabindex="5" name="title" />
    			</fieldset>

    to post to custom field: fname ?

Viewing 10 replies - 1 through 10 (of 10 total)
  • You cannot just “post to a custom field”. You have to explicitly save each custom form field to the wordpress system, check: https://codex.www.ads-software.com/Function_Reference/update_post_meta

    Thread Starter broadleon

    (@broadleon)

    So far I have this on the form,

    But im a litle lost when it comes to posting to custom fields.

    <?php
    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 a title';
    	}
    	if (isset ($_POST['description'])) {
    		$description = $_POST['description'];
    	} else {
    		echo 'Please enter the content';
    	}
    
    	$tags = $_POST['post_tags'];
    
    	// ADD THE FORM INPUT TO $new_post ARRAY
    	$new_post = array(
    	'post_title'	=>	$title,
    	'post_content'	=>	$description,
    	'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
    	'tags_input'	=>	array($tags),
    	'post_status'	=>	'publish',           // Choose: publish, preview, future, draft, etc.
    	'post_type'	=>	'log',
    	);
    
    	//SAVE THE POST
    	$pid = wp_insert_post($new_post);
    
                 //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    	wp_set_post_tags($pid, $_POST['post_tags']);
    
    	//REDIRECT TO THE NEW POST ON SAVE
    	$link = get_permalink( $pid );
    	wp_redirect( $link );
    
    	//ADD OUR CUSTOM FIELDS
    	//add_post_meta($pid, 'rating', $winerating, true); 
    
    } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
    
    //POST THE POST YO
    do_action('wp_insert_post', 'wp_insert_post');
    
    ?>

    After this

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

    You can save the custom fields in this manner:

    //SAVE THE POST
    $pid = wp_insert_post($new_post);
    
    if($pid)
    {
    update_post_meta($pid, "fname", sanitize_text_field( $_POST['fname'] ) );
    }
    Thread Starter broadleon

    (@broadleon)

    Ah ! I see, Thankyou.

    You’re welcome.

    Thread Starter broadleon

    (@broadleon)

    Might of been a Little premature, as that didn’t work, i did find another way,

    //SAVE THE POST
    	$pid = wp_insert_post($new_post);
    
                 //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    	wp_set_post_tags($pid, $_POST['post_tags']);
    
    	//REDIRECT TO THE NEW POST ON SAVE
    	$link = get_permalink( $pid );
    	wp_redirect( $link );
    
    	//ADD OUR CUSTOM FIELDS
    	add_post_meta($pid, 'fname', $fname, true);

    that didn’t work either.

    Are you certain that the field names match with your system?
    I just guessed and put names for you to understand. You have to make sure to change the field names.

    Thread Starter broadleon

    (@broadleon)

    Ive tried to keep is simple in advanced custom fields i created fname and in the form i create fname, so fname -> fname. But Ive now noticed my title field is blank on creation of new post

    Thread Starter broadleon

    (@broadleon)

    got the title back, made a error in name of input. NOW IT WORKS!

    <!-- First Name -->
    			<fieldset name="fname">
    				<label for="fname">First Name:</label>
    				<input type="text" id="fname" value="" tabindex="5" name="title" />
    			</fieldset>

    Great!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘custom form to post to custom field’ is closed to new replies.