最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - Update Post meta with custom variable

programmeradmin1浏览0评论

I Just want to update (edit existing) the custom meta box variable from front-end. I got a form Front end post editing using a form & process things but in this code post-title i can edit & update, but i cannot edit & update values custom metabox variable values.

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; ?>

I Just want to update (edit existing) the custom meta box variable from front-end. I got a form Front end post editing using a form & process things but in this code post-title i can edit & update, but i cannot edit & update values custom metabox variable values.

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; ?>
Share Improve this question edited Jul 15, 2019 at 6:45 Noufal Binu asked Jul 13, 2019 at 8:48 Noufal BinuNoufal Binu 2713 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

That is easy, you are trying to update the wrong value.

Please replace the following

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']) );

With this:

$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 );
}

You should be pushing the values as an array in _zon_testimonial_key key.

发布评论

评论列表(0)

  1. 暂无评论