1

I created a metabox into my posts, it's a checkbox that I checked.

Here's the code for saving the data :

 add_action('save_post','save_metaboxes');
 function save_metaboxes($post_ID){

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    //Don't update on Quick Edit
    if (defined('DOING_AJAX') ) {
        return $post_id;
    }

    if(isset($_POST[ 'is_viewpay' ])) {
        update_post_meta($post_ID, '_is_viewpay', 'active');
    } else {
        update_post_meta($post_ID, '_is_viewpay', '');
    }

} 

The problem is :

It's working when I manually set my post from scheduled to published (click on publish button)

But when the post automatically published, my meta value does not save at all.

I don't know why it's not working..

Thanks for your help !

3
  • Do you mean the _is_viewpay is empty or does not appear in DB table at all?
    – Max Yudin
    Commented Dec 13, 2017 at 10:26
  • It's on a scheduled. Commented Dec 13, 2017 at 10:30
  • @MaxYudin It's empty Commented Dec 13, 2017 at 10:58

1 Answer 1

0

I had a similar problem with a custom metabox. The problem was the use of $post_id inside the save-function. Using global $post and then $post->ID solved the problem for me.

function save_metabox(){
    global $post;

    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
        return $post->ID;
    }

    $myData = $_POST['my data'];
    update_post_meta($post->ID;, $myData);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.