New listing API isn't working

I’ve tried the suggestions here, but to no effect. Instead of a listing, a post is being created.

import requests
from requests.auth import HTTPBasicAuth

url = “https://domain.com/wp-json/wp/v2/posts/
username = “user”
password = “password”

Construct the authorization header

auth_header = HTTPBasicAuth(username, password)

Define the data to be sent in the POST request

data = {
‘title’: ‘Yahoo.com’,
‘content’: ‘some test description’,
‘status’: ‘publish’,
‘categories’: [5], # Assuming category ID 5
‘meta’: {
‘post_type’: ‘hp_listing’
}
}

Make the POST request

response = requests.post(url, auth=auth_header, json=data)

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.

Can anyone help please.

Can any of the devs suggest anything please?

Is this project abandoned?

For those who are struggling with the same. I was able to activate the api post for this theme by adding the following to the theme’s function php:

function create_hp_listing_post_type() {
    register_post_type('hp_listing', array(
        'labels' => array(
            'name' => __('Listings'),
            'singular_name' => __('Listing')
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'listing'),
        'show_in_rest' => true, 
        'rest_base' => 'hp_listings', 
        'rest_controller_class' => 'WP_REST_Posts_Controller', 
        
    ));
}

add_action('init', 'create_hp_listing_post_type');

But the taxonomies, categories etc need to be activated as well, so I am researching that now.

1 Like

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.

Hope this helps.

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?

Please I beg you don’t make me wait another month for a reply lol I need a clue and I can handle the rest.

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.

Ihor,

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.

Ihor,

I saw a notification, but don’t see any addition. Did you modify the code or am I missing something?

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.

Is there a way we can get a reply before the holidays?

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.

I tried that, created the listing, pulled the id of the listing, tried to update the metas to no effect. I give up. Good luck with your theme.

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.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.