Support

Account

Home Forums ACF PRO ACF REST api image only shows attachment id Reply To: ACF REST api image only shows attachment id

  • Hi @ashisharyal64gmail-com

    I think I had similar but found you can alter the API output. For me, I needed to customise the API response for WooCommerce, here’s the code I used which may help:

    // Custom API Data
    function product_compact_post( $data, $post, $request ) {
    	
    	$categories = get_the_term_list( $data->data['id'], 'product_cat' );	
    	$categories = strip_tags( $categories );	
    
    	$product   = wc_get_product( $data->data['id'] );
    	$image_id  = $product->get_image_id();
    	$image_url = wp_get_attachment_image_url( $image_id, 'full' );		
    
    	$brands = get_the_term_list( $data->data['id'], 'product_brand' );	
    	$brands = strip_tags( $brands );
    	
    	$model = get_field('model', $data->data['id']);
    	
    	$sku = str_replace( array(' ', '&', '/'), array('_', '', ''), $model );		
    	
    	return [
    		'id'		=> $data->data['id'],
    		'sku'		=> $data->sku = $sku,
    		'name'		=> $data->data['title']['rendered'],
    		'link'		=> $data->data['link'],
    		'categories'=> $data->categories = $categories,
    		'image' 	=> $data->image = $image_url,		
    		'manufacturer'	=> $data->manufacturer = $brands,
    		'model'		=> $data->model = $model,
    		'mpn' 		=> $data->mpn = $model,
    		'ean' 		=> $data->ean = get_field('ean', $data->data['id'])		
    	];    
        return $data;
    }
    add_filter( 'rest_prepare_product', 'product_compact_post', 10, 3 );

    You may be able to edit for your needs.

    Hope that helps!