• Hello,

    I’ve used a hook named as save_post which run when post is updated or published, but this hook does not getting value from custom meta box. Here is My code, Can someone help with that?

    add_action('save_post','save_floorplan_property_callback',10,3);
    function save_floorplan_property_callback($post_id, $post, $update){
        global $wpdb;
        if ($post->post_type != 'property'){
            return;
        }
        
        $floorplan = get_field( 'find_on_the_ground', $post_id );
        if($floorplan == "No"){
          //do something
        } else {
          //do something
        }
    }

    Default Value is No and it always go in No Condition.

    Thanks

    • This topic was modified 5 years, 4 months ago by t-p. Reason: fixed code

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • The save_post hook is called in multiple situations. It is when the post is being created, and when an auto-save is done, and when the post is updated.
    The post meta is saved after the post, because it uses the post ID as a key.
    So if your code is only for the case when the post is being created, you won’t have any post meta in the database.

    Thread Starter chetansoul1993

    (@chetansoul1993)

    Thanks @joyously for quick Reply.

    My Code is for 2 situation, For update as well as for a new post. But it’s not working even for the update as well.

    Also can you tell me what can be the possible solution for checking the condition on basis of acf custom meta box.

    Requesting to seek additional comments for the same.

    Thanks in Advance.

    I misspoke about the timing of the hook and the post meta. The post is saved, the taxonomies are updated, the post meta is saved, the status is transitioned, and then the save_post action is called.
    See the code at: https://developer.ww.wp.xz.cn/reference/functions/wp_insert_post/

    So you really should ask your question at the support forum for ACF (whose code defines get_field()), because I don’t know which hook that plugin uses to save the extra custom fields.

    Thread Starter chetansoul1993

    (@chetansoul1993)

    I am closing this ticket as it is resolved.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom Field Not working with WordPress hooks’ is closed to new replies.