I’ve seen multiple post but I can’t find a solution.
I’m trying to create listing from the REST API, but I cannot set the attributes with the value I want, that works for the main title, description, but anything I tried with the attributes doesn’t work.
Can you help me ?
Here is a sample of the request body:
{
"title": "My test",
"content": "This is the description of the listing.",
"type": "hp_listing",
"status": "publish",
"meta": {
"hp_price": 546,
"hp_title": "The attributes title"
}
}
Here is one of the attributes I want to configure:
AFAIK, hp_price already exists for listing natively. And you try to add a custom attribute with the same name. Unless I misunderstood.
Could be the reason why.
If you already tested creating a listing and it works, please make sure that WordPress doesn’t require anything extra for setting meta fields. I found this article and it seems that WordPress requires registering each meta field to be available via REST API WordPress REST API: Update Post Meta The meta key itself is correct, if the field is named “price” then the meta key is “hp_price”.
Quick updates,
I finally succeeded to update custom fields (it’s needed to register each meta fields as @ihor sugested it)
My only problems now is I do not succeed to change the value of array fields.
Selectable fields are implemented as custom taxonomy terms, while regular fields are implemented as post meta. For example, if you have a Select attribute named “brand”, then options for listings should be assigned as terms of “hp_listing_brand” taxonomy (you can check the available terms via the Edit Options button of the attribute).
It does help, but should I register each taxonomy like I did for each meta fields ?
Because I tried to just add them in the body (outside of the meta object and with the “hp_listing” prefix and it doesn’t change
No, you don’t have to register it - if this attribute is created in Listings/Attributes, then the taxonomy “hp_listing_[attribute_name_here]” should be already available. Please make sure that you list terms correctly, I found this topic php - Assign custom taxonomy to post with REST API - Stack Overflow
As far as I understand, I added the terms into my body, set the custom taxonomy as explained on the link you provided, but I still not succeed to make it works. Any idea or things I missed ?
The code seems to be ok, but I recommend setting terms by ID, this may work. Also, you can try to error_log the result of wp_set_object terms, if there’s an error it will be clear, e.g. if the taxonomy name is not correct or the taxonomy is not available yet at the time the code runs.
Yes, please check the term ID in the URL when you edit this option, it looks like “…tag_ID=123…”. Maybe WP REST API requires term IDs instead of slugs, this may work.
This is a WordPress-level issue, if you’re using the WP REST API and the available registered post types / taxonomies (“hp_listing” and “hp_listing_[field_name]”, depending on the attribute name) please try debugging this further using error_log.
Thank you @ihor , registering the taxonomies works fine and I succeeded to add the whole details of my object.
Last things I need to do is to add the images. I succeeded to do it by creating the post and then use the /attachments request to add them one by one, but I wanted to know if there is a fields where I can just put the Id of the images and add them while creating the post ?
Thank you in advance.
PS: Once finished, I’ll create a post with all the code (snippets, request, …) I’ve add to make it works, in order to simplify everything for other people