Support

Account

Home Forums General Issues get_field in functions.php

Solved

get_field in functions.php

  • Working in the functions.php file to send an email when a custom post type is published.

    If i hard code in the value for the post id it works fine and i can get the value of the post id set to $ID but when I try and insert the variable $ID into the get_field function it returns nothing.

    function notify_client_on_publish( $new_status, $old_status, $post ) {
            
        if ( 'publish' !== $new_status || $new_status === $old_status || 'clientnotes' !== get_post_type( $post ) ) {
            return;
        }
    
        if ( ! $user = get_userdata( $post->post_author ) ) {
            return;
        }
    
        $ID = $post->ID;
        $post_meta_user  = get_post_meta($ID, 'client', true);
        $client = get_field('client', $ID);
        
        $body = sprintf( "<%s> latest_cpt <%s>", $client, $ID );
    
        wp_mail( $user->user_email, 'New Message from VSB Performance!', $body );
        
    }
    add_action( 'transition_post_status', 'notify_client_on_publish', 20, 3 );
  • Same for the get_post_meta function. I can’t seem to pass the ID to that either. Either function would work if I could figure out why the ID cant be passed as a variable.

  • 
    $post_meta_user  = get_post_meta($ID, 'client', true);
    $client = get_field('client', $ID);
    

    What type of field is client?
    What is the return format of that field?

  • The value that would return for client would be the value of this select: so 990003

    <select id=”acf-field_63b1e4a196201″ class=”select2-hidden-accessible” name=”acf[field_63b1e4a196201]” data-ui=”1″ data-multiple=”0″ data-placeholder=”Select” data-allow_null=”0″ data-query-nonce=”aa8b4f40ca” tabindex=”-1″ aria-hidden=”true” data-ajax=”1″>
    <option value=”990003″ selected=”selected” data-i=”0″>[email protected] (Hoppes User)</option>
    </select>

  • I ended up tapping into the save_post hook and then the meta info was available

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.