admin管理员组

文章数量:1122846

I am trying to update the value of a custom field (order) inside a POST when it is saved on the back office. If my status is Sold, I want the value of order to be 3. Everything is working smoothly according to my logs, even the function returns true, but the value does not change as it should.

Here is my code:

function update_on_save($post_id) {
    error_log("fonction post_id :".$post_id);
     // Enregistrement automatique = on sort
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if ($post_id && get_post_type($post_id) == 'post') {
        if(in_category('Orquera Listings',$post_id)) {
            $status = get_post_meta($post_id,'status', true);   
            error_log("status :".$status);

            switch ($status) {
                case 'Pending':
                    $order = 1;
                    break;
                 case 'For Sale':
                    $order = 2;
                    break;
                case 'Sold':
                    $order = 3;
                    break;
                default:
                    $order = 4;
            }
            
            /*$old_order = get_post_meta($post_id,'order', true);
            result = update_post_meta($post_id, 'order', $order,$old_order);*/
            error_log("order : ".$order);
            
            //update_field('order', $order, $post_id);
                        
            $result = update_post_meta($post_id, 'order', $order);
            error_log('result :'.$result);
            
            
            //global $wpdb;    
            //$result = $wpdb->get_results( "update FROM wp_usermeta set meta_value =  WHERE meta_key = 'points' AND user_id = '1'");
            //$wpdb->update($wpdb->prefix.'postmeta', array('meta_value' => $order), array('post_id' => $post_id,'meta_key' => 'order'));
            //$order_db = get_post_meta($post_id,'order', true);    
            //error_log('order_db :'.$order_db);
        }
    }
}
add_action('save_post', 'update_on_save', 10,3);
//add_action('wp_after_insert_post', 'update_on_save', 100,3);

I let everything in comment to show some stuff I tried, but I tried a lot of different hooks. I have the impression that I have a timing problem and my update is replaced by the actual save.

Thanks in advance!

本文标签: hooksupdate ACF field when saving post on back office