Support

Account

Home Forums General Issues acf/pre_save_post with a Post Object

Solved

acf/pre_save_post with a Post Object

  • I am using the acf/pre_save_post filter to pass title and description from a custom field.

    I now want to do the same using a post object field however that it more difficult as it just keeps passing the title field into the input.

    Here is the code in my functions file:

    function acf_review_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_5dd41181a90d9'];
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_5dd537b5ecddf'];
    	$_POST['acf']['s'] = $_POST['acf']['field_5dd54240e53b7'];
    
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
    
  • and which one of those fields is the post object field?

    A post object field stores a post ID and does not store the post title of the post that is selected. If you want to store information about that other post then you will need to get that other post, it is also a string in $)POST and not an INT

    
    $post = get_post(intval($_POST['acf']['key_for_post_object_field']));
    // use title of post
    $value = $post->post_title;
    
  • Hi John,

    Thanks for your reply! That was stupid of me to not point out which field is the post object.

    It’s the final line:

    $_POST['acf']['s'] = $_POST['acf']['field_5dd54240e53b7'];

    ‘s’ is the name of the input in wp/admin area that I want to fill with the input from the acf field name which is the custom post object input in my acf new post form on the front-end.

  • Please also note that the first two are text fields and are working as expected.

  • Here is what happens with the first two:

    1- acf title field on front-end fills the post title

    2- acf description field on front-end fills the post description

    What I want to happen with the third is:

    3- acf post object input fills an input in the post

  • You need to know the acf key of that other field and you can set it. Just like your example, but use the field key where you have “s”

    
    $_POST['acf']['s'] = {value to set};
    

    as I noted, the acf post object field will have a string value of the post ID. If you want to populate the other field with the ID then you’d just copy that value over. If you want to use other information about the post then you’ll need to get the post and get that other information as I explained above. In all cases, you need to provide a value that is in the same format that ACF is expecting to be submitted by the field you’re trying to auto populate.

  • But that field ‘s’ isn’t an ACF field so it doesn’t have an ACF key. It is a field in my theme with the name ‘s’. I want the acf field to post the id into the post field named ‘s’. Both of the field values when I inspect are the id of the post.

  • ACF will not automatically insert values for custom fields for other plugins or the theme.

    You need to figure out how/where that value is stored and use the correct WP function to update that value, i.e. update_post_meta(), update_option(), etc.

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

You must be logged in to reply to this topic.