custom post types - Use a Variable in update_post_meta as the $meta_key

looking at the example on wordpressupdate_post_meta( $post_id, $meta_key, $meta_value, $prev_value );I seem to be having

looking at the example on wordpress

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.

looking at the example on wordpress

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.

Share Improve this question asked Jul 5, 2019 at 5:06 MFWebMasterMFWebMaster 114 bronze badges 6
  • try $week_s1 = '_member_score'.$week.'_s1'; – Chetan Vaghela Commented Jul 5, 2019 at 5:45
  • Hi Thank but no joy still not working – MFWebMaster Commented Jul 5, 2019 at 6:06
  • remove $_POST[$s1] and add just $s1 in update_post_meta($pidac, $week_s1 , $s1); – Chetan Vaghela Commented Jul 5, 2019 at 6:27
  • Nope still no joy in get it working – MFWebMaster Commented Jul 5, 2019 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 Commented Jul 5, 2019 at 10:56
 |  Show 1 more comment

1 Answer 1

Reset to default 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";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745343818a4623452.html

相关推荐

  • custom post types - Use a Variable in update_post_meta as the $meta_key

    looking at the example on wordpressupdate_post_meta( $post_id, $meta_key, $meta_value, $prev_value );I seem to be having

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信