Home Forums General Issues Cannot update title field based on post type


Cannot update title field based on post type

  • I have a piece of code in functions that updates the WP default title of a post on creation with the same value as an ACF field. This works fine when I do it one time. However if I try to apply this same logic but for different post types it will not work:

    function acf_review_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    			if ( 'post' == get_post_type() ) {
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_610b936e9292c'];
    			elseif ( 'customposttype' == get_post_type() ) {
    			    	$_POST['acf']['_post_title'] = $_POST['acf']['field_61ddb5d56ddfe']; 
    	return $post_id;
    add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
  • You need to use $post_id in get_post_type()

    if ('post' == get_post_type($post_id) {

    I’m surprised that it ever worked.

  • @hube2 I have added in $post_id and it does not work unfortunately.

  • Are you including the ACF title field in the acf_form() that it’s not working on?

  • @hube2 yes 100%. This is working if I remove the post type conditions and only do it for ‘post’ or for my custom post type alone. But I cannot get it to work with more than one post type.

  • So it works if you only have this

    if ('post' == get_post_type($post_id)) {
      $_POST['acf']['_post_title'] = $_POST['acf']['field_610b936e9292c'];

    or if you only have this

    if ('customposttype' == get_post_type($post_id)) {
      $_POST['acf']['_post_title'] = $_POST['acf']['field_61ddb5d56ddfe']; 

    but it does not work when you have this

    if ('post' == get_post_type($post_id)) {
      $_POST['acf']['_post_title'] = $_POST['acf']['field_610b936e9292c'];
    } elseif ('customposttype' == get_post_type($post_id)) {
      $_POST['acf']['_post_title'] = $_POST['acf']['field_61ddb5d56ddfe']; 

    I don’t see anything in the code that you’ve provided that can cause this.

  • @hube2 none of those. Just if I remove the custom post type condition all together.

  • okay, this is a DUH on my part. get_post_type() cannot return anything useful here. This is a acf/pre_save_post. At the point that this is run the post has not been created yet so therefore it cannot have a post type.

    You need to look at something else to figure out what field to use in the title.

  • @hube2 I see thanks. Any indication as to what that could be? I don’t imagine I’m the only person that has needed to update the title field of more than one post type considering ACF forms does not support core WP fields.

  • I personally tend to using acf/save_post filters and updating the title after the post is created and the acf fields populated.

    In this case I would look for the existence of the field

    if (!empty($_POST['acf']['field_610b936e9292c'])) {
    } elseif (!empty($_POST['acf']['field_61ddb5d56ddfe']) {
  • @hube2 ok I see, and then in the result do this?:

    $_POST['acf']['_post_title'] = $_POST['acf']['field_610b936e9292c'];

    I’ll give it a go. Thank you 🙂

  • @hube2 thanks so much that works perfectly! 🙂

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

You must be logged in to reply to this topic.