我只想从前端更新(编辑现有的)自定义meta-box变量。我有张表格Front end post editing using a form &;处理事情,但在此代码帖子标题中,我可以编辑(&P);更新,但我无法编辑(&A);更新值自定义metabox变量值。
global $current_user;
wp_get_current_user();
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'update_post_nonce\']) && isset($_POST[\'postcontent\']) )
{
$post_id = $_POST[\'post_id\'];
$post_type = \'testimonial\';
$capability = ( \'page\' == $post_type ) ? \'edit_page\' : \'edit_post\';
if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST[\'update_post_nonce\'], \'update_post_\'. $post_id ) )
{
$post = array(
\'ID\' => esc_sql($post_id),
\'post_content\' => esc_sql($_POST[\'postcontent\']),
\'post_title\' => esc_sql($_POST[\'post_title\'])
);
wp_update_post($post);
if ( isset($_POST[\'package\']) ) update_post_meta($post_id, \'zon_package\', esc_sql($_POST[\'zon_package\']) );
if ( isset($_POST[\'phone\']) ) update_post_meta($post_id, \'zon_phone\', esc_sql($_POST[\'zon_phone\']) );
}
else
{
wp_die("You can\'t do that");
}
}
$args = array(
\'post_type\' => \'testimonial\',
\'post_status\' => \'publish\',
\'post_per_page\' => 5,
\'author\' => $current_user->ID,
\'meta_query\' => array(
array(
\'key\' => \'_zon_testimonial_key\',
\'value\' => \'s:8:"approved";i:1;s:8:"featured";i:0;\',
\'compare\' => \'LIKE\'
)
)
);
$query =new WP_Query($args);
if ($query->have_posts() || is_user_logged_in() || current_user_can(\'edit_post\', $args->ID)) :
while ($query->have_posts()) : $query->the_post();
echo \'<p>\'.get_the_title().\'</p>\';
?>
<form class="royal_page" role="form" method="post">
<input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
<?php wp_nonce_field( \'zon_testimonial\'. get_the_ID(), \'zon_testimonial_nonce\' );?>
<p><label for="post_title">Title</label>
<input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p>
<p><?php wp_editor( $post->post_content, \'postcontent\' ); ?></p>
<p><label for="post_title">Package</label>
<?php $value = get_post_meta(get_the_ID(), \'_zon_testimonial_key\', true); ?>
<input type="text" id="zon_package" name="zon_package" value="<?php echo esc_attr($value["package"]); ?>" /></p>
<p><label for="post_title">Phone</label>
<?php $value = get_post_meta(get_the_ID(), \'_zon_testimonial_key\', true); ?>
<input type="text" id="zon_phone" name="zon_phone" value="<?php echo $value["phone"]; ?>" /></p>
<input type="submit" id="submit" value="Update" />
</form>
<?php endwhile; endif; ?>
SO网友:Faham Shaikh
这很容易,因为您试图更新错误的值。
请更换以下部件
if ( isset($_POST[\'package\']) ) update_post_meta($post_id, \'zon_package\', esc_sql($_POST[\'zon_package\']) );
if ( isset($_POST[\'zon_phone\']) ) update_post_meta($post_id, \'phone\', esc_sql($_POST[\'phone\']) );
使用此选项:
$zon_package = \'\';
$zon_phone = \'\';
if ( isset($_POST[\'package\']) ) $zon_package = esc_sql($_POST[\'zon_package\']);
if ( isset($_POST[\'zon_phone\']) ) $zon_phone = esc_sql($_POST[\'phone\']);
$zon_testimonial_key = array();
if(isset($zon_package) && isset($zon_phone)) {
$zon_testimonial_key = array (
\'package\' => $zon_package,
\'phone\' => $zon_phone
);
if(!empty($zon_testimonial_key)) {
update_post_meta($post_id, \'_zon_testimonial_key\', $zon_testimonial_key );
}
您应该将值作为数组推送到
_zon_testimonial_key
钥匙