1
<ul>
<?php
        $vArgs = array(
            'posts_per_page' => -1,
            'offset' => 0,
            'order' => 'DESC',
            'orderby'  => 'date',
            'post_type' => 'video-list',
            'post_status' => 'publish',
            'suppress_filters' => 0
        );
        $video_list = get_posts($vArgs);
        if ($video_list) {
            $count = 1;
            foreach ($video_list as $video_item) {?>
            <li>
                <div class="admin_video_views_val bottom_view_col">
                    <input type="text" name="addon_post_views_count_meta[<?php echo $count; ?>]" id="addon_post_views_count_meta[<?php echo $count; ?>]" class="get_addon_post_views_count" value="<?php echo $post_views_countnew;?>">
                </div>
            </li>
            <?php
            $count++; 
            }
        } ?>
  </ul>

I created admin menu and am showing all posts views count there in input fields values with this code:

<input type="text" name="addon_post_views_count_meta[<?php echo $count; ?>]" id="addon_post_views_count_meta[<?php echo $count; ?>]" class="get_addon_post_views_count" value="<?php $post_views_count = get_post_meta( $video_item->ID, 'post_views_count', true );if ( ! empty( $post_views_count ) ) {echo $post_views_count;}else{echo $post_views_countnew;}?>">

How you see I put input text fields it means that count needs to be editable by user, so when user changes one of the values it shouls save that value now. I wrote something like this but it doesn't work properly, it doesn't save the option but should do, how can I do this?

if(isset($_POST['addon_post_views_count_meta'])){
    for($i=0; $i<count($_POST['addon_post_views_count_meta']); $i++){
         if($_POST['addon_post_views_count_meta'][$i] >= 0){
             $post_views_countnew = esc_attr($_POST['addon_post_views_count_meta'][$i]);   
             update_option($_POST['addon_post_views_count_meta'][$i], $post_views_countnew);               
         }
    }
}

for($i=0; $i<count($_POST['addon_post_views_count_meta']); $i++){
    if($_POST['addon_post_views_count_meta'][$i] >= 0){
        $post_views_countnew = get_option($_POST['addon_post_views_count_meta'][$i]);
        echo $post_views_countnew;
    }
} 

Your Answer

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

Browse other questions tagged or ask your own question.