-1

looking at the example on wordpress.org

update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );

I seem to be having a problem replacing $meta_key with a variable. here is my code

Part 1

Setting the $week

<form action="#" method="post">
<select name="Week">
<option value="week_1">1</option>
<option value="week_2">2</option>
<option value="week_3">3</option>
<option value="week_4">4</option>
<option value="week_5">5</option>
<option value="week_6">6</option>
<option value="week_7">7</option>
<option value="week_8">8</option>
</select>
<button class="btn btn-lg btn-primary" type="submit" name="submit" value="Selected a Week" >Selected a Week</button>
</form>
<?php
if(isset($_POST['submit'])){
$week = $_POST['Week'];  // Storing Selected Value In Variable
echo "You have selected : ".$week." to be edited";  // Displaying Selected Value
}
?>

If you echo $week all is ok.

Part 2

$current_post   =   $post->ID;
$title          =   get_the_title();
$s1             =   $title . "s1";

$week_s1        =   "_member_score_".$week._s1";
$post_information = array(
// Look at this //
'ID'            =>  $current_post,
'post_title'    =>  $title,
'post-type'     =>  'CTP-Name',
'post_status'   =>  'publish',
);
$pidac = wp_update_post($post_information);
if($pidac)
{
update_post_meta($pidac, $week_s1, $_POST[$s1]);
$term_ids = array( 2, 7 );
$taxonomy = 'Custom-Taxonomy';
wp_set_object_terms( $pidac, $term_ids, $taxonomy );
// Redirect
wp_redirect( home_url() );
}
}

If you echo $week_s1 all is ok

If you replace the the variable $week_s1 with the typed out value "_member_score_week_1_s1" the code works perfectly.Here is the example

update_post_meta($pidac, "_member_score_week_1_s1" , $_POST[$s1]);

Can someone please advise me on how I can use the ($week_s1 ) variable in this code.

  • try $week_s1 = '_member_score'.$week.'_s1'; – Chetan Vaghela Jul 5 at 5:45
  • Hi Thank but no joy still not working – MFWebMaster Jul 5 at 6:06
  • remove $_POST[$s1] and add just $s1 in update_post_meta($pidac, $week_s1 , $s1); – Chetan Vaghela Jul 5 at 6:27
  • Nope still no joy in get it working – MFWebMaster Jul 5 at 7:50
  • what kind of value is $_POST[get_the_title()."s1"] ? there is no corresponding post key for that in your form... (ps post-type should be post_type) – majick Jul 5 at 10:56
0

There is an error in setting variable on this line:

$week_s1="_member_score_".$week._s1";

this line should be

$week_s1 ="_member_score_".$week."_s1";
  • I fix the code but still no joy. – MFWebMaster Jul 5 at 12:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.