WordPress.org

Ready to get started?Download WordPress

Forums

Trouble modifying post metakey value from frontend (4 posts)

  1. erikshosting
    Member
    Posted 2 years ago #

    Hi, I am trying to use a radio form to modify a custom metakey value for posts. The posts wont have the metakey assigned at first, so I will use update_post_meta for its fallback capability to add_post_meta.. but I am having trouble with something simple!
    Anyone see whats wrong? my code gets the value correctly, but doesnt update the metakey on submit via button or with my fancy onclicks...

    //Function to create radio buttons to change post metakey tentativeastatus
    function display_tentative_approval_buttons() {
    	    Global $post;
    	$tentativeastatus = get_post_meta($post->ID, 'tentativeastatus', true);
    	//check if pre-approved
    	if ($tentativeastatus == "pre-approved") { $tentativeastatusp == "checked"; }
    	//check if waitlisted
    	if ($tentativeastatus == "waitlisted") { $tentativeastatusw == "checked"; }
    	//check if denied
    	if ($tentativeastatus == "denied") { $tentativeastatusd == "checked"; }
    	//display current status
    	echo '<small><u>Currently: ' . $tentativeastatus . '</u></small><hr />';
    	//start the form
    	echo '<form method="post" action="">' . wp_nonce_field('update_wcepsa_postmeta','wcepsa_inventory') . '
    <input type="radio" name="tentativeastatus" value="pre-approved" onClick="submit();" ' . $tentativeastatusp . ' /> Pre-Approved<br />
    <input type="radio" name="tentativeastatus" value="waitlisted" onClick="submit();" ' . $tentativeastatusw . ' /> Waitlisted<br />
    <input type="radio" name="tentativeastatus" value="denied" onClick="submit();" ' . $tentativeastatusd . ' /> Denied <br />
    <small>Click to Modify</small>
    </form>';
    }
    
    //change tentative post status
    if ( isset( $_POST['wcepsa_inventory'] ) && wp_verify_nonce($_POST['wcepsa_inventory'],'update_wcepsa_postmeta') )
        { //if nonce check succeeds.
            global $post;
            $postid = $post->ID;
            $data = $_POST['tentativeastatus'];
            update_post_meta($postid,'tentativeastatus',$data);
        }

    anyone see why it wont save?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    The postid is not set. Put this in your form:

    <input type="hidden" name="post_id" value="'.$post->ID.'" />

    Now you can update post meta with:

    $postid = $_POST['post_id'];
    $data = $_POST['tentativeastatus'];
    update_post_meta($postid,'tentativeastatus',$data);
  3. erikshosting
    Member
    Posted 2 years ago #

    Oh man you rock! That definitely solved the non-updating issue... it works like a charm.

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. Glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic