0

i want to delete post meta with same key like 'rtf_booking' but this key have store data in array like

Array ( [0] => first booking [1] => second booking [2] => third booking )

i want to delete with only first booking or selected booking if click third booking then only delete third booking in array with same post ID and key, how this is possible, any help.

my code.

add_action( 'add_meta_boxes', 'rtf_booking_meta_box' );
add_action( 'wp_ajax_rtf_delete_post', 'rtf_delete_post' );
function rtf_booking_meta_box(  ){

add_meta_box( 
    'rtf_booking',
    __( 'booking' , ' rtf'),
    'rtf_og_booking',
    'rtf_booking_pc',
    'normal',
    'default'
);
}

function rtf_og_booking{
 ?>
        <input type="hidden" id="rtf_post" value="<?php echo $post->ID; ?>">
        <button class="del_rtf btn ">delete</button>
 <?php
} 

ajax code.

$(document).on('click', '.del_rtf', function(e) {
    e.preventDefault();

    var post_id = $('#rtf_post').val();
      var data = {
        'post_id': post_id,
        'action': 'rtf_delete_post',
        }
    $.post(ajaxurl, data, function(resp){   
        console.log(resp);

    });

});

ajax callback function

    function rtf_delete_post(){

    $postid = isset($_POST['post_id']) ? $_POST['post_id'] : '';
    $status = delete_post_meta($postid, 'rtf_og_booking') ? 'Success' : 'Error';

    die($status);

}
  • 1
    You need to get the meta as a an array in PHP, change the value, then save it again. Worth noting that this is one of many reasons that storing this information this way is a very bad idea. Are you in control of how the data is stored? – Jacob Peattie Jan 24 '18 at 6:34
  • how i store data in best way with same key and post id – rahil ahmed Jan 24 '18 at 6:49
  • do you have any idea please update my code . – rahil ahmed Jan 24 '18 at 6:50
  • Jacob Peattie ???? – rahil ahmed Jan 24 '18 at 8:53

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.