Support

Account

Home Forums Front-end Issues Change acf_form field label & description based on post type

Helping

Change acf_form field label & description based on post type

  • I know you can use acf/prepare_field to alter a field, but wondering if there’s a way to change it based on the post type it’s adding/updating. For example, have a custom post type named ‘custom_type’ and want to alter the title label to ‘My custom type label’ without affecting other acf_form’s. So something like:

    function acf_title_field( $field ) {
      if ( POST_TYPE == 'custom_type' ) {
        $field['label'] = "My custom type label";
      }
    
      return $field;
    }
    add_filter('acf/prepare_field/name=_post_title', 'acf_title_field');
  • The problem you would run into is that there is no post ID for a new post and this would be the only way to get the post type.

    If it is for an existing post the you could do

    
    global $post;
    $post_type = get_post_type($post->ID);
    

    But like I said, for a new post this will not work because there is no post ID to get and if you get it you’re probably getting the wrong post ID.

    In this case, I would add my prepare_field hooks directly to the template file where I am including the acf_form() call and have a different filter for each post type where I want to alter the labels and instructions so that I do not need to depend on the global $post value.

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

The topic ‘Change acf_form field label & description based on post type’ is closed to new replies.