Home › Forums › ACF PRO › ACF REST api image only shows attachment id › Reply To: ACF REST api image only shows attachment id
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!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
✨ You can now install ACF PRO with Composer, eliminating the need for third-party installers. Get the details and instructions here. https://t.co/ebEfp60Pwj
— Advanced Custom Fields (@wp_acf) February 2, 2023
© 2023 Advanced Custom Fields.
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.