Check if the request was successful (status code 200)
if response.status_code == 201: # 201 Created is the expected status code for a successful POST request
print(“Post created successfully!”)
print(response.json())
else:
print(f"Error: {response.status_code}")
print(response.text)
I tried post, post_type. I tried post_type outside meta. It always creates a regular post, not a listing.
Why the most important API action has no direct end point is beyond me.
I’m sorry for the late reply, there were delays with developer-specific issues but it should be ok now, HivePress is not abandoned for sure.
Yes, if you use the core WP REST API then you have to enable show_in_rest for the listing post type, other parameters can be removed. We also plan to add the built-in endpoint for creating listings so using the core API will not be needed.
Thank you for the reply. I am still having issues passing the attributes via API. I was able to enable categories/taxonomy as well, but can’t figure out how to pass attributes when creating a listing. Can you point me towards a solution?
If you use the core WP API then listing is a custom post type, so listing attributes should be passed as meta values and taxonomy terms. For example, if you added a “phone” attribute in HivePress, then it’s stored as post meta “hp_phone” for the “hp_listing” post type. Similarly, if you added a selectable attribute “foobar” then options are stored as taxonomy terms of “hp_listing_foobar” taxonomy.
I’ve tried that myself before and it didn’t work. The listing gets created, but the attribute fields inside the listing are empty. I’ve selected simple text attributes to start with and I was using this:
# Define the data to be sent in the POST request, with added meta values
data = {
'title': 'Yahoo.com',
'content': 'some test description',
'status': 'publish',
'hp_listing_categories': [5],
'type': 'hp_listing', # Specify the custom post type here
'meta': {
'hp_sub_type': 'Investment',
'hp_discovered': 'November 12, 2023',
},
}
which is similar to what you are suggesting I think. Am I doing it wrong? Is it possible that since the attributes are custom post types that they have to be somehow enabled for rest api? I have enabled hp_listing api and the taxonomy api and they work, just can’t pass the attributes.
A reply would be awesome. Also stuck there, getting all requests or listings is no issue, however getting the custom attributes don’t seem to work for neither of those on my side.
Sorry for the delay. Currently it’s the only way to create listings via REST API, if you already enabled “show_in_rest” for the “hp_listing” post type then the code you posted should be ok, maybe extra permissions are required to update meta at the same time while creating the post object? If I remember correctly this required admin authentication. Please try updating meta in a separate request.
You can also test this further by checking the wp_postmeta database table, if entries are not added there then it’s a WP-level API issue. But in any case, we’ll add plugin-level API endpoints to avoid using the WP-level API. Thanks for your feedback and patience.