Don’t have access to post meta in save_post action

So what I’ve noticed is that if I check for a meta value in my save_post function, it comes as blank, meaning the meta value hasn’t actually been entered into the post. Looking at this code for example,

function order_mirror_create($post_id) {

    global $post;

    if($post->ID == ''){
            $pid = $post_id;
    } else {
            $pid = $post->ID;
    }

    $videohost = get_post_meta($pid, 'video_provider', true);


    if ($videohost == "UploadAnime") {

        add_post_meta($pid, 'video_display_order', 1, true);

    } else {

        add_post_meta($pid, 'video_display_order', $videohost, true);

    }

}

$videohost is actually blank, so get_post_meta($pid, 'video_provider', true) does not return the actual value of that meta field because it hasn’t been created yet.

Read More

So in the code above, the else is always the one that runs, since $videohost is blank. How can I fix this? What action should I use that would run in such a manner that the post meta have already been added to the post.

Related posts

Leave a Reply

1 comment

  1. You should change the following:

    if ($videohost == "UploadAnime") {
    
            add_post_meta($pid, 'video_display_order', 1, true);
    
        } else {
    
            add_post_meta($pid, 'video_display_order', $videohost, true);
    
        }
    

    To:

    if ($videohost == "UploadAnime") {
    
            update_post_meta($pid, 'video_display_order', 1, true);
    
        } else {
    
            update_post_meta($pid, 'video_display_order', $videohost, true);
    
        }
    

    I find add_post_meta has never really worked for me, whereas update_post_meta does.