Support

Account

Home Forums ACF PRO ACF REST api image only shows attachment id

Solved

ACF REST api image only shows attachment id

  • Hi,

    Im using Nuxt to do the front end and ACF is vital for what I am doing

    As the topic suggests, when you attach an image, you only get media id on the response, which is fine if that is what you want. Im working with ACF pro where I have selected Image URL as output format but I get the image ID.

    When I select Image Array, same thing happens, I only get image id. Example is below

    "acf": {
        "header_image": 45,
      },

    I was using ACF to REST API plugin as the output there was correct. You would get what you select with that plugin.

    I think allowing users to select media output type will let us save some time. If you just want to display image, you would select image url and that it. You dont have to do another request and filter through the response. It would save us some coding time.

    If there is anyway to get image url to work, I would really appreciate it.

    Thank you for awesome plugin.
    Ashish A

  • 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!

  • Hi @jarvis ,
    Thanks for quick response, I will go through this and update here accordingly.

    Ashish A.

  • This is resolved!

    Above reply gave me some insight on how I may be able to do this creating custom filter and i thank you for that. But I realize there was a very easy fix to this. Below is the source:
    https://www.advancedcustomfields.com/resources/wp-rest-api-integration/#rest-api-field-types

    so basically, if you attach ?acf_format=standard at the end of your request so it looks like the following https://localhost.local/wp-json/wp/v2/pages/45?acf_format=standard , You will get image urls or array or id, as you selected while configuring the field.

    I hope this help.

    Ashish A.

  • Nice one and thanks for the update. I needed a custom API due to requiring specific info but glad you found a solution too.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.