featured-image-by-url icon indicating copy to clipboard operation
featured-image-by-url copied to clipboard

How can add the "featured-image-by-url" in a post using the rest API?

Open BaggioMarco opened this issue 3 years ago • 5 comments

Hi,

I tried to create a new post using the REST API, but the "featured-image-by-url" doesn't not display.

I tried different endpoint like _knawatfibu_url, featured_image_urls, jetpack_featured_media_url but I had not luck. I installed also the plugin "Rest API Custom Fields" that used to enable the custom field '_knawatfibu_url' under the 'post' section, but the image did not show up.

Below an example of code I was using.

imageURL = 'https://upload.wikimedia.org/wikipedia/it/f/fc/Pippo_Disney.png'

content= {
    "title": 'Hi',
    "content": 'My Content',
    "date": '2020-08-17T10:16:34',
    "featured_image": , # IMPORTANT
    '_knawatfibu_url': imageURL ,
    'featured_image_urls': imageURL ,
    'jetpack_featured_media_url': imageURL ,

}

thanks in advance

BaggioMarco avatar Jan 15 '22 12:01 BaggioMarco

Marco, you must put urls in metadata, like here:

{"name": " your_productname ", "type": "simple", "sku": " your_productsku ", "regular_price": " your_price ", "manage_stock": true, "on_sale": true, "stock_quantity": your_stock, "images": ""', "short_description": " your_shortdescription ", "description": "your_description ", "slug": " your_productslug ", "meta_data": [ {"key": "_knawatfibu_url", "value": " your_featuredimage_url " }, {"key": "_knawatfibu_wcgallary", "value": " your_galleryimage_url1, your_galleryimage_url2, your_galleryimage_url3 " }, ]};

All names started with "your" are wildcards for your data. Pay special attention to quotation marks.

fernandogiacomino avatar Jan 15 '22 13:01 fernandogiacomino

Thanks @fernandogiacomino

I used your wocommerce example for a normal post, but the image is not showing up. Here the code I used:

post = { 'title': 'Hi2', 'content': 'Content', # "meta_data": [{"key": "_knawatfibu_url", "value":"https://upload.wikimedia.org/wikipedia/it/f/fc/Pippo_Disney.png"}], "meta_data": {"key": "_knawatfibu_url", "value":"https://upload.wikimedia.org/wikipedia/it/f/fc/Pippo_Disney.png"}, 'status': "publish", }

I've tried also the commented line, but no luck. I tried both with the "Rest API Custom Fields" enabled (with the field "_knawatfibu_url" checked) and disabled, but no luck.

What's wrong with my code? Thanks in advance

BaggioMarco avatar Jan 15 '22 15:01 BaggioMarco

I found the solution here: https://stackoverflow.com/questions/56743795/wordpress-api-post-meta-field

BaggioMarco avatar Jan 15 '22 18:01 BaggioMarco

Yes, I didn't realize that your query was related to a Wordpress post and not a Woocommerce product. Greetings!

fernandogiacomino avatar Jan 17 '22 20:01 fernandogiacomino