Support

Account

Home Forums General Issues Using get_field in acf/save_post

Solved

Using get_field in acf/save_post

  • Hi there.

    I have a custom post type that does not use the default post title. Instead, I have four Name fields that, together, make up the post title. I’m trying to use acf/save_post to set the post_title based on the values of these four fields, but something is going wrong and get_field is returning empty.

    Here’s a simplified version of what I have:

    
    add_action( 'acf/save_post', 'biwp_fix_contributor_title', 20 );
    
    function biwp_fix_contributor_title( $post_id ) {
        $post_type = get_post_type( $post_id );
        
        if ( 'contributors' == $post_type ) {
            $gnames = get_field( 'biwp_gnames', $post_id );
            $snames = get_field( 'biwp_snames', $post_id );
    
            $title = $gnames . $snames; 
    
            $data = array(
                'ID'         => $post_id,
                'post_title' => $title,
                'post_name'  => sanitize_title( $title ),
            );
    
            wp_update_post( $data );
        }
    }
    

    But this returns empty. But if I replace the $title with a string, for example, it works—same if I use the $post_id. So it’s get_field that’s not doing it.

    If I use error_log( print_r( $gnames, true ) ); (on any name variable) I get an empty line in my debug log, but it also doesn’t flag the variables as undefined. I’ve tried every variation of get_field I could think of—using the field key instead of the field name, with or without $post_id—but the results are the same regardless. Any ideas?

  • What type of fields are biwp_gnames and biwp_snames?

  • Both are basic text fields.

  • I am having a similar issue in v 5.6.1 with get_field returning false when run inside an acf/save_post-hook.

    Any ideas what could cause this?

  • No ideas at all. If these are basic text fields and not inside a repeater or something like that then I don’t see anything wrong with the code. I’m not seeing a problem on any of my own sites that do something similar. I just built a post type that gets the post title form an acf field using almost exactly the same code.

    I would look at what could be interfering with it, like other plugins or other filters in the theme by switching themes and deactivating plugins.

  • Thanks for checking my code, John. I went through the painstaking process of disabling all plugins and re-enabling them one by one, and testing at each point.

    For anyone else having this problem:
    I found that the plugin W3 Total Cache was the one causing problems. On further testing, I found that it was specifically the Object Cache setting that was the culprit. When I disabled that, I could update the post title as expected.

    Whew!

  • Yes, checking plugins can be a painstaking process, but with the number of possibilities, is really the only way to narrow down a cause.

    Caching plugins, unfortunately, can cause a problem. I don’t use caching plugins that run in the admin of a site, so I don’t generally think of these.

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

You must be logged in to reply to this topic.