Support

Account

Home Forums General Issues Including ACF data for media

Solving

Including ACF data for media

  • Hello,

    I have successfully been able to update ACF for custom post types using the REST API. However, when trying the same method for media, it hasn’t worked.

    Can you insert ACF data for media using the REST API? If so, what approach do you take?

    This is the code I’m using

    const route =${API_URL}media/${id}`;
    const body = {
    status: ‘publish’,
    fields: {
    job_code: ${job_code},
    },
    };

    const res = await fetch(route, {
    method: ‘POST’,
    headers: {
    ‘Content-Type’: ‘application/json’,
    Authorization: Bearer ${token},
    },
    body: JSON.stringify(body),
    });`

  • @ycrad Did you manage to find a solution to this? I am experiencing the exact same issue.

    Updating ACF fields to post types other than media works fine, but doesn’t with the same approach applied to the media endpoint.

  • @pickerro98 No, unfortunately I never found a solution for this. We ended up not using ACF as a result!

  • “Media” is a post just like any other post with a post type of “attachment”. You would add ACF field to it the same as you add them to any other post.

  • While “Media” is definitely of post type “attachment” there is no route at /wp-json/wp/v2/attachment/

    When submitting a GET to /wp-json/wp/v2/media/, an empty array of acf is shown for each object. Even with fields populated, no data is returned. In addition, a POST to a specific media item at /wp-json/wp/v2/media/<id> to update a custom field does not work in the same way it does for other post types/custom post types.

    Is there an alternate method available of updating data in ACF fields groups shown on media/attachments?

  • @aswhipple My reply was just a guess. I don’t use the REST for updating fields and I don’t have any resources to point you to. Seems to me that it must be something in WP and treating attachments differently than other post types.

  • Fair enough, @hube2!

    We were able to bypass this issue by manually exposing the field:

    function our_expose_attribution() {
    	// Field name to register.
    	$field = 'attribution';
    	register_rest_field(
    		'attachment',
    		$field,
    		array(
    			'get_callback' => function ( $object ) use ( $field ) {
    				// Get field as single value from post meta.
    				return get_post_meta( $object['id'], $field, true );
    			},
    			'update_callback' => function ( $value, $object ) use ( $field ) {
    				// Update the field/meta value.
    				update_post_meta( $object->ID, $field, $value );
    			},
    			'schema' => array(
    				'type' => 'string',
    				'arg_options' => array(
    					'sanitize_callback' => function ( $value ) {
    						// Make the value safe for storage.
    						return sanitize_text_field( $value );
    					},
    					'validate_callback' => function ( $value ) {
    						// Validate
    						return is_string( $value );
    					},
    				),
    			),
    		)
    	);
    }
    add_action( 'rest_api_init', 'our_expose_attribution' );
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.