Create a listing with REST API and custom fields

Hello,

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:

Thanks in advance

Salut @nicho_dev !

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.

My two cents.

1 Like

Hey @condorito.fr ,

Thanks for the reply.

I tried the same with another custom attribute names “hp_power” and I got the same result

Hi,

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”.

Hope this helps

1 Like

Hi,

Thank you for the help @ihor

I’m currently trying multiple things for registering each meta field but without any success at the moment. I’ll let you know if something evolve.

In the meantime, I’m still open for any suggestions :slight_smile:

Thanks

Quick updates,
I finally succeeded to update custom fields :party_popper: (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.

If anyone has any tips ?

1 Like

Please let me know what you mean about the array fields, images or repeatable fields?

By array fields, I mean a custom fields which you select via a drop-down list in the admin.

As an example I created a dropdown list named brand with multiple options, on the admin side I select it like this (see picture)

This is the kind of data I don’t succeed to change

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).

Hope this helps

1 Like

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 ?

There is the custom taxonomy setting

There is my attributes:

There is one of my attributes options:

There is my terms in the body:

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.

By settings terms by ID, you mean in the body request to change it by the id of the option selected?

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.

I tried and it doesn’t 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.

You can also try this solution, I noticed that an extra parameter is needed to make taxonomies available in WP REST API Adding REST API Support For Custom Content Types – REST API Handbook | Developer.WordPress.org You can add show_in_rest parameter using register_taxonomy_args hook.

Hope this helps

1 Like

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 :smiley: