Support

Account

Home Forums General Issues Automatically populate custom field with post ID

Solved

Automatically populate custom field with post ID

  • I have a custom post type and I need one of its custom fields to automatically have the post id of that post. How can I do this?

  • I’m not sure the field type, but you can populate a select field like this. Place the following in your functions.php file.

    
    function acf_load_field_choices( $field ) {
    
      	// Create an empty array
    	$field['choices'] = array();
    
    	// Populate field with the current post id
    	$field['choices'][0] = get_the_ID();
    
      	// Return the result
    	return $field;
    
    }
    
    add_filter('acf/load_field/name=test', 'acf_load_field_choices');
    
  • The type I’m using is a simple text field. This snippet helped me. Thank you!

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

The topic ‘Automatically populate custom field with post ID’ is closed to new replies.